Example usage for javax.swing.table DefaultTableModel DefaultTableModel

List of usage examples for javax.swing.table DefaultTableModel DefaultTableModel

Introduction

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

Prototype

public DefaultTableModel(Object[][] data, Object[] columnNames) 

Source Link

Document

Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method.

Usage

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  . jav  a 2 s.c om*/
        }
    };
    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... s) {

    DefaultTableModel model = new DefaultTableModel(4, 4) {
        @Override/*from  w w  w. j a v a2s  .c o m*/
        public boolean isCellEditable(int row, int column) {
            return column == 3;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndex == 3) {
                return Boolean.class;
            }
            return super.getColumnClass(columnIndex);
        }
    };

    JTable t = new JTable(model);
    JFrame f = new JFrame();
    f.add(new JScrollPane(t));

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    Object[][] rowData = {};/*  ww w.ja  va2s  . 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:Main.java

public static void main(String args[]) {
    DefaultTableModel model;/*from   w w  w .  j  av  a2  s.  com*/
    JTable t = new JTable(model = new DefaultTableModel(0, 1));
    for (int i = 0; i < 10; i++) {
        model.addRow(new Object[] { i });
    }
    JButton removeSelected = new JButton("remove");
    removeSelected.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int[] selectedRows = t.getSelectedRows();
            for (int i = selectedRows.length - 1; i >= 0; i--) {
                model.removeRow(selectedRows[i]);
                ;
            }
        }
    });
    JFrame f = new JFrame();
    f.add(new JScrollPane(t));
    f.add(removeSelected, BorderLayout.SOUTH);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.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();
        }/* w w  w .  ja  v a 2  s.c o m*/
    };
    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: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);//from  w  ww .ja va 2 s . co  m
    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) {
    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[] {});
        }/* w ww . j a  v a2  s  .  com*/
    });
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(new JScrollPane(table));
    panel.add(addRow, BorderLayout.SOUTH);
    JOptionPane.showMessageDialog(null, panel);
}

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 ww. j  a va  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);
}

From source file:Main.java

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

    JTable table;/*from www  .  j  a  v a 2 s  .  c  om*/
    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:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Sorting JTable");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object rows[][] = { { "A", "A", 1 }, { "E", "E", 4 }, { "Y", "Y", 3 } };
    String columns[] = { "Symbol", "Name", "Price" };
    TableModel model = new DefaultTableModel(rows, columns) {
        public Class getColumnClass(int column) {
            Class returnValue;/*  ww  w  .  j  a  v a  2  s .c  o  m*/
            if ((column >= 0) && (column < getColumnCount())) {
                returnValue = getValueAt(0, column).getClass();
            } else {
                returnValue = Object.class;
            }
            return returnValue;
        }
    };

    JTable table = new JTable(model);
    RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    table.setRowSorter(sorter);
    JScrollPane pane = new JScrollPane(table);
    frame.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}