Example usage for javax.swing JTable JTable

List of usage examples for javax.swing JTable JTable

Introduction

In this page you can find the example usage for javax.swing JTable JTable.

Prototype

public JTable() 

Source Link

Document

Constructs a default JTable that is initialized with a default data model, a default column model, and a default selection model.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    JTable table = new JTable();
    int vColIndex = 0;
    TableColumn col = table.getColumnModel().getColumn(vColIndex);
    col.setHeaderRenderer(new MyTableHeaderRenderer());

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable();
    table.getColumnModel().addColumnModelListener(new MyTableColumnModelListener(table));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable();
    TableColumn col = table.getColumnModel().getColumn(0);
    col.setCellEditor(new MyTableCellEditor());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable();
    DefaultTableModel model = (DefaultTableModel) table.getModel();

    model.addColumn("A", new Object[] { "item1" });
    model.addColumn("B", new Object[] { "item2" });

    String[] values = new String[] { "1", "2", "3" };

    TableColumn col = table.getColumnModel().getColumn(0);
    col.setCellEditor(new MyComboBoxEditor(values));
    col.setCellRenderer(new MyComboBoxRenderer(values));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable();
    DefaultTableModel model = (DefaultTableModel) table.getModel();

    model.addColumn("A", new Object[] { "item1" });
    model.addColumn("B", new Object[] { "item2" });

    String[] values = new String[] { "1", "2", "3" };
    TableColumn col = table.getColumnModel().getColumn(0);
    col.setCellEditor(new SpinnerEditor(values));
}

From source file:Main.java

public static void main(String[] args) {
    Object[][] rowData = { { "Hello", "World" } };
    Object[] columnNames = { "A", "B" };

    JTable table;//from ww w.  j  av a 2 s  . com
    DefaultTableModel model;

    model = new DefaultTableModel(rowData, columnNames);
    table = new JTable();
    table.setModel(model);
    JButton add = new JButton("Add");

    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            model.addRow(rowData[0]);
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = f.getContentPane();
    c.setLayout(new BorderLayout());
    c.add(add, BorderLayout.SOUTH);
    c.add(new JScrollPane(table), BorderLayout.CENTER);

    f.pack();

    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:invoice.GetInvoice.java

/**
 * @param args the command line arguments
 *///from  ww w.j av a  2s. c o  m
public static void main(String[] args) {

    NumberFormat nf = NumberFormat.getInstance();
    nf.setGroupingUsed(false);
    nf.setMaximumFractionDigits(2);
    nf.setMinimumFractionDigits(2);
    nf.setRoundingMode(RoundingMode.HALF_UP);

    try {
        JSONObject arg_json = new JSONObject(args[0]);
    } catch (JSONException ex) {
        Logger.getLogger(GetInvoice.class.getName()).log(Level.SEVERE, null, ex);
    }
    HashMap<String, Object> hm = new HashMap<>();
    hm.put("duplicate", "");
    hm.put("distributor", "//oshan" + "\n" + "//kapuhempala" + "\n\nArea: " + "//galle");
    hm.put("customer", "//owner" + "\n" + "//Agro" + "\n" + "//Agro add" + "\n" + "//0771894851");
    hm.put("invNo", "GSLTS" + String.format("%04d", Integer.parseInt("//100")));
    hm.put("invDate", "2014-01-10");
    hm.put("invCode", "300");

    double invoiceTotal = 500000;
    if (5 > 0) {//ShopDiscount
        double discountprice = (invoiceTotal * 99) / 100;//getShopDiscount()
        hm.put("invoiceDiscount", nf.format((invoiceTotal) * 99 / 100));//getRetail_discount()

    } else {
        hm.put("invoiceDiscount", "");
    }
    hm.put("gross_total", nf.format(invoiceTotal));
    hm.put("invoiceTotal", nf.format(((invoiceTotal) * (100 - 99) / 100)));//getRetail_discount()
    hm.put("salesPersonName", "rep");
    hm.put("salesPersonContactNo", "0772189584");

    JTable jTable1 = new JTable();
    jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {},
            new String[] { "ITEMCODE", "DESCRIPTION", "QTY", "FREEQTY", "PRICE", "AMOUNT" }));
    String reportSource = "./ireports/invoice.jrxml";
    DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
    try {
        JasperReport jr = JasperCompileManager.compileReport(reportSource);
        JasperPrint jp = JasperFillManager.fillReport(jr, hm, new JRTableModelDataSource(dtm));
        JasperPrintManager.printReport(jp, false);
    } catch (JRException ex) {
        Logger.getLogger(GetInvoice.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("1");
}

From source file:ExpenseReport.java

public ExpenseReport() {
    super("Expense Report");
    setSize(570, 200);//from  www  .ja  va  2 s.c o  m

    m_data = new ExpenseReportData(this);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);
    m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    for (int k = 0; k < ExpenseReportData.m_columns.length; k++) {
        TableCellRenderer renderer;
        if (k == ExpenseReportData.COL_APPROVED)
            renderer = new CheckCellRenderer();
        else {
            DefaultTableCellRenderer textRenderer = new DefaultTableCellRenderer();
            textRenderer.setHorizontalAlignment(ExpenseReportData.m_columns[k].m_alignment);
            renderer = textRenderer;
        }

        TableCellEditor editor;

        if (k == ExpenseReportData.COL_CATEGORY)
            editor = new DefaultCellEditor(new JComboBox(ExpenseReportData.CATEGORIES));
        else if (k == ExpenseReportData.COL_APPROVED)
            editor = new DefaultCellEditor(new JCheckBox());
        else
            editor = new DefaultCellEditor(new JTextField());

        TableColumn column = new TableColumn(k, ExpenseReportData.m_columns[k].m_width, renderer, editor);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();
    ps.setSize(550, 150);
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

    ImageIcon penny = new ImageIcon("penny.gif");
    m_title = new JLabel("Total: $", penny, JButton.LEFT);
    m_title.setForeground(Color.black);
    m_title.setAlignmentY(0.5f);
    p.add(m_title);
    p.add(Box.createHorizontalGlue());

    JButton bt = new JButton("Insert before");
    bt.setMnemonic('b');
    bt.setAlignmentY(0.5f);
    ActionListener lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            m_data.insert(row);
            m_table.tableChanged(
                    new TableModelEvent(m_data, row, row, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
            m_table.repaint();
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    bt = new JButton("Insert after");
    bt.setMnemonic('a');
    bt.setAlignmentY(0.5f);
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            m_data.insert(row + 1);
            m_table.tableChanged(new TableModelEvent(m_data, row + 1, row + 1, TableModelEvent.ALL_COLUMNS,
                    TableModelEvent.INSERT));
            m_table.repaint();
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    bt = new JButton("Delete row");
    bt.setMnemonic('d');
    bt.setAlignmentY(0.5f);
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            if (m_data.delete(row)) {
                m_table.tableChanged(new TableModelEvent(m_data, row, row, TableModelEvent.ALL_COLUMNS,
                        TableModelEvent.INSERT));
                m_table.repaint();
                calcTotal();
            }
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    getContentPane().add(p, BorderLayout.SOUTH);

    calcTotal();

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
}

From source file:StocksTable3.java

public StocksTable3() {
    super("Stocks Table");
    setSize(600, 300);/* w w  w  .  ja  v a2  s. c  o m*/

    m_data = new StockTableData();

    m_title = new JLabel(m_data.getTitle(), new ImageIcon("money.gif"), SwingConstants.LEFT);
    m_title.setFont(new Font("TimesRoman", Font.BOLD, 24));
    m_title.setForeground(Color.black);
    getContentPane().add(m_title, BorderLayout.NORTH);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);

    for (int k = 0; k < StockTableData.m_columns.length; k++) {
        DefaultTableCellRenderer renderer = new ColoredTableCellRenderer();
        renderer.setHorizontalAlignment(StockTableData.m_columns[k].m_alignment);
        TableColumn column = new TableColumn(k, StockTableData.m_columns[k].m_width, renderer, null);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:StocksTable4.java

public StocksTable4() {
    super("Stocks Table");
    setSize(600, 300);//from   ww w. ja va2 s. c  o m

    m_data = new StockTableData();

    m_title = new JLabel(m_data.getTitle(), new ImageIcon("money.gif"), SwingConstants.LEFT);
    m_title.setFont(new Font("TimesRoman", Font.BOLD, 24));
    m_title.setForeground(Color.black);
    getContentPane().add(m_title, BorderLayout.NORTH);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);

    for (int k = 0; k < StockTableData.m_columns.length; k++) {
        DefaultTableCellRenderer renderer = new ColoredTableCellRenderer();
        renderer.setHorizontalAlignment(StockTableData.m_columns[k].m_alignment);
        TableColumn column = new TableColumn(k, StockTableData.m_columns[k].m_width, renderer, null);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(true);
    header.addMouseListener(m_data.new ColumnListener(m_table));
    header.setReorderingAllowed(true);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}