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(TableModel dm) 

Source Link

Document

Constructs a JTable that is initialized with dm as the 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 {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    model.addColumn("Col1");
    model.addColumn("Icon Here");

    table.getColumnModel().getColumn(0).setHeaderValue(new ImageIcon("image.gif"));
    table.getColumnModel().getColumn(0).setHeaderRenderer(new IconHeaderRenderer());
}

From source file:Main.java

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

    model.addColumn("Col1");
    model.addColumn("Col2");

    table.getTableHeader().getColumnModel().getColumn(1).setHeaderRenderer(new IconHeaderRenderer());

    table.getColumnModel().getColumn(1).setHeaderValue(new TextAndIcon("Col2", new ImageIcon("icon.gif")));
}

From source file:Main.java

public static void main(String[] args) {
    String[][] data = { { "Data", "Data" } };
    String[] col = { "Col", "Col" };
    final DefaultTableModel model = new DefaultTableModel(data, col);
    JTable table = new JTable(model);
    JButton addRow = new JButton("Add Empty Row");
    addRow.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.addRow(new Object[] {});
        }/*from   ww  w .j ava  2s  . com*/
    });
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(new JScrollPane(table));
    panel.add(addRow, BorderLayout.SOUTH);
    JOptionPane.showMessageDialog(null, panel);
}

From source file:Main.java

public static void main(String[] args) {
    int ROW_HEIGHT = 40;
    String[] TABLE_COLUMNS = { "Foo", "Bar" };
    DefaultTableModel tableModel = new DefaultTableModel(TABLE_COLUMNS, 2);
    JTable table = new JTable(tableModel);
    table.setRowHeight(ROW_HEIGHT);// w  w  w. ja v a2  s  .  c  om
    JScrollPane scrollpane = new JScrollPane(table);
    JButton addRowBtn = new JButton(new AbstractAction("Add Row") {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            tableModel.addRow(new String[] { "", "" });
        }
    });
    JPanel btnPanel = new JPanel();
    btnPanel.add(addRowBtn);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(scrollpane, BorderLayout.CENTER);
    f.getContentPane().add(btnPanel, BorderLayout.PAGE_END);
    f.pack();
    f.setLocationByPlatform(true);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().setLayout(new BorderLayout());

    MyTableModel model = new MyTableModel();

    JTable table = new JTable(model);
    table.setRowHeight(80);//ww  w  .  j av a 2s. c  o m
    table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());
    JScrollPane pane = new JScrollPane(table);
    frame.getContentPane().add(BorderLayout.CENTER, pane);
    frame.setSize(500, 400);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DefaultTableModel model = new DefaultTableModel(null, new String[] { "CheckMe", "Value" }) {
        public Class getColumnClass(int c) {
            switch (c) {
            case 0:
                return Boolean.class;
            default:
                return String.class;
            }//w w w .  j  a  v  a2s. co  m
        }
    };
    JTable table = new JTable(model);
    JFrame frame = new JFrame("CheckBox Test");
    frame.add(table);
    model.addRow(new Object[] { true, "This is true" });
    model.addRow(new Object[] { false, "This is false" });
    frame.pack();
    frame.validate();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    Object[][] rowData = {};/* w  ww .ja  va  2  s  . c o m*/
    Object[] columnNames = { "Column 1", "Column 2", "Column 3" };

    DefaultTableModel listTableModel;
    listTableModel = new DefaultTableModel(rowData, columnNames);
    for (int i = 1; i < 25; i++) {
        String rowString = "Quiz #" + i;
        listTableModel.addRow(new Object[] { rowString, "ICON", "ICON" });
    }

    JTable listTable;
    listTable = new JTable(listTableModel);
    listTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    listTable.setCellEditor(null);
    listTable.setBounds(37, 143, 397, 183);

    JFrame frame = new JFrame();
    frame.add(new JScrollPane(listTable));
    frame.setVisible(true);
    frame.pack();
}

From source file:SparseTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Sparse Test");
    String headers[] = { "English", "Japanese" };
    TableModel model = new SparseTableModel(10, headers);
    JTable table = new JTable(model);

    model.setValueAt("one", 0, 0);
    model.setValueAt("ten", 9, 0);
    model.setValueAt("roku - \u516D", 5, 1);
    model.setValueAt("hachi - \u516B", 8, 1);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);//from w  ww  .j  a va2s.c om
    frame.setVisible(true);
}

From source file:JTableSortDemo.java

public static void main(String[] args) {
    Object[][] data = { { "A", 5 }, { "B", 2 }, { "C", 4 }, { "D", 8 } };
    String columnNames[] = { "Item", "Value" };
    TableModel model = new DefaultTableModel(data, columnNames) {
        public Class<?> getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }/*from   w  ww . j av a2s .c om*/
    };
    JTable table = new JTable(model);

    TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    table.setRowSorter(sorter);

    JScrollPane scrollPane = new JScrollPane(table);
    JFrame frame = new JFrame("Sorting Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:JTableFilterDemo.java

public static void main(String[] args) {
    Object[][] data = { { "A", 5 }, { "B", 2 }, { "C", 4 }, { "D", 8 } };
    String columnNames[] = { "Item", "Value" };
    TableModel model = new DefaultTableModel(data, columnNames) {
        public Class<?> getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }/*w  w w. ja  v  a 2 s. c  o m*/
    };
    JTable table = new JTable(model);

    RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
        public boolean include(Entry entry) {
            Integer population = (Integer) entry.getValue(1);
            return population.intValue() > 3;
        }
    };

    TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    sorter.setRowFilter(filter);
    table.setRowSorter(sorter);
    JScrollPane scrollPane = new JScrollPane(table);
    JFrame frame = new JFrame("Filtering Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane);
    frame.setSize(300, 200);
    frame.setVisible(true);
}