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:ChooserTableSample.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Editable Color Table");
    TableModel model = new ColorTableModel();
    JTable table = new JTable(model);

    TableColumn column = table.getColumnModel().getColumn(3);

    ComboTableCellRenderer renderer = new ComboTableCellRenderer();
    column.setCellRenderer(renderer);// www  .  ja va  2 s . com

    TableCellEditor editor = new ColorChooserEditor();
    column.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
}

From source file:ColorTableModel.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Editable Color Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    TableModel model = new ColorTableModel();
    JTable table = new JTable(model);
    // TableColumn column = table.getColumnModel().getColumn(3);
    // column.setCellRenderer(renderer);
    // column.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);/* w w  w  . j  av a  2 s . c  om*/
    frame.setVisible(true);
}

From source file:EditableColorColumn.java

public static void main(String args[]) {

    Color choices[] = { Color.red, Color.orange, Color.yellow, Color.green, Color.blue, Color.magenta };
    ComboTableCellRenderer renderer = new ComboTableCellRenderer();
    JComboBox comboBox = new JComboBox(choices);
    comboBox.setRenderer(renderer);/*from  w w w  .j a v a2  s  .  c  o m*/
    TableCellEditor editor = new DefaultCellEditor(comboBox);

    JFrame frame = new JFrame("Editable Color Table");
    TableModel model = new ColorTableModel();
    JTable table = new JTable(model);

    TableColumn column = table.getColumnModel().getColumn(3);
    column.setCellRenderer(renderer);
    column.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
}

From source file:RegexTable.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Regexing JTable");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object rows[][] = { { "A", "About", 44.36 }, { "B", "Boy", 44.84 }, { "C", "Cat", 463.63 },
            { "D", "Day", 27.14 }, { "E", "Eat", 44.57 }, { "F", "Fail", 23.15 }, { "G", "Good", 4.40 },
            { "H", "Hot", 24.96 }, { "I", "Ivey", 5.45 }, { "J", "Jack", 49.54 }, { "K", "Kids", 280.00 } };
    String columns[] = { "Symbol", "Name", "Price" };
    TableModel model = new DefaultTableModel(rows, columns) {
        public Class getColumnClass(int column) {
            Class returnValue;/*from   w  w  w  .  ja va  2s.c o  m*/
            if ((column >= 0) && (column < getColumnCount())) {
                returnValue = getValueAt(0, column).getClass();
            } else {
                returnValue = Object.class;
            }
            return returnValue;
        }
    };

    final JTable table = new JTable(model);
    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    table.setRowSorter(sorter);
    JScrollPane pane = new JScrollPane(table);
    frame.add(pane, BorderLayout.CENTER);

    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Filter");
    panel.add(label, BorderLayout.WEST);
    final JTextField filterText = new JTextField("A");
    panel.add(filterText, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);
    JButton button = new JButton("Filter");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = filterText.getText();
            if (text.length() == 0) {
                sorter.setRowFilter(null);
            } else {
                sorter.setRowFilter(RowFilter.regexFilter(text));
            }
        }
    });
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 250);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) {

    JFrame demoFrame = new JFrame("Variable Row Height Table Demo");

    StringTableModel imageTableModel = new StringTableModel();

    JTable imageTable = new JTable(imageTableModel);
    imageTable.getColumnModel().getColumn(0).setCellRenderer(new VariableRowHeightRenderer());

    demoFrame.getContentPane().add(new JScrollPane(imageTable));

    demoFrame.pack();// www.  j a v a  2s.c  om
    demoFrame.setVisible(true);
}

From source file:SparseTableModel.java

public static void main(String[] a) {

    String headers[] = { "A", "B" };
    TableModel model = new SparseTableModel(10, headers);
    JTable table = new JTable(model);
    model.setValueAt("1", 0, 0);
    model.setValueAt("2", 9, 0);
    model.setValueAt("3", 5, 1);
    model.setValueAt("4", 8, 1);

    JFrame frame = new JFrame("Fixed Column Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new JScrollPane(table), BorderLayout.CENTER);
    frame.setSize(300, 150);//w  w w.  j a v  a 2s  .  c om
    frame.setVisible(true);
}

From source file:FixedColumnModel.java

public static void main(String args[]) {

    final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };

    final String columnNames[] = { "#", "English", "Roman" };

    final TableModel fixedColumnModel = new AbstractTableModel() {
        public int getColumnCount() {
            return 1;
        }/*from   w  w w. ja  v  a  2s.  c om*/

        public String getColumnName(int column) {
            return columnNames[column];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column];
        }
    };

    final TableModel mainModel = new AbstractTableModel() {
        public int getColumnCount() {
            return columnNames.length - 1;
        }

        public String getColumnName(int column) {
            return columnNames[column + 1];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column + 1];
        }
    };

    JTable fixedTable = new JTable(fixedColumnModel);
    fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JTable mainTable = new JTable(mainModel);
    mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    ListSelectionModel model = fixedTable.getSelectionModel();
    mainTable.setSelectionModel(model);

    JScrollPane scrollPane = new JScrollPane(mainTable);
    Dimension fixedSize = fixedTable.getPreferredSize();
    JViewport viewport = new JViewport();
    viewport.setView(fixedTable);
    viewport.setPreferredSize(fixedSize);
    viewport.setMaximumSize(fixedSize);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
    scrollPane.setRowHeaderView(viewport);

    JFrame frame = new JFrame("Fixed Column Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:ColumnSample.java

public static void main(String args[]) {
    TableModel model = new AbstractTableModel() {
        Icon icon1 = new ImageIcon("TreeCollapsed.gif");

        Icon icon2 = new ImageIcon("TreeExpanded.gif");

        Object rowData[][] = { { "1", "ichi", Boolean.TRUE, new Date("01/01/2000"), icon1 },
                { "2", "ni", Boolean.TRUE, new Date("04/15/1999"), icon2 },
                { "3", "san", Boolean.FALSE, new Date("12/07/1941"), icon2 },
                { "4", "shi", Boolean.TRUE, new Date("02/29/2000"), icon1 },
                { "5", "go", Boolean.FALSE, new Date("05/23/1995"), icon1 }, };

        String columnNames[] = { "English", "Japanese", "Boolean", "Date", "ImageIcon" };

        public int getColumnCount() {
            return columnNames.length;
        }/*from  w w w  .  j a  va  2s.c  o  m*/

        public String getColumnName(int column) {
            return columnNames[column];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column];
        }

        public Class getColumnClass(int column) {
            return (getValueAt(0, column).getClass());
        }
    };

    JFrame frame = new JFrame("Column Renderer Table");
    JTable table = new JTable(model);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
}

From source file:TableSample2.java

public static void main(String args[]) {
    Object rows[][] = { { "one", "ichi - \u4E00", "un" }, { "two", "ni - \u4E8C", "deux" },
            { "three", "san - \u4E09", "trois" }, { "four", "shi - \u56DB", "quatre" },
            { "five", "go - \u4E94", "cinq" }, { "six", "roku - \u516D", "treiza" },
            { "seven", "shichi - \u4E03", "sept" }, { "eight", "hachi - \u516B", "huit" },
            { "nine", "kyu - \u4E5D", "neuf" }, { "ten", "ju - \u5341", "dix" } };
    Object options[] = { "un", "deux", "trois", "quatre", "cinq", "treiza", "sept", "huit", "neuf", "dix" };
    JComboBox comboBox = new JComboBox(options);
    comboBox.setMaximumRowCount(4);//from   w w w  .j a  v a2  s  .c  o  m
    TableCellEditor editor = new DefaultCellEditor(comboBox);

    Object headers[] = { "English", "Japanese", "French" };
    JFrame frame = new JFrame("JTable Anatomy");
    class CustomTableModel extends DefaultTableModel {
        public CustomTableModel(Object rowData[][], Object columnNames[]) {
            super(rowData, columnNames);
        }

        public Class getColumnClass(int col) {
            Vector v = (Vector) dataVector.elementAt(0);
            return v.elementAt(col).getClass();
        }

        public boolean isCellEditable(int row, int col) {
            return true;
        }
    }
    JTable table = new JTable(new DefaultTableModel(rows, headers));

    //    ColumnModelUtilities.removeHeaders(table.getColumnModel());
    table.getColumnModel().getColumn(2).setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:EditableColumn.java

public static void main(String args[]) {
    TableModel model = new AbstractTableModel() {
        Icon icon1 = new ImageIcon("TreeCollapsed.gif");

        Icon icon2 = new ImageIcon("TreeExpanded.gif");

        Object rowData[][] = { { new Integer(1), "ichi", Boolean.TRUE, new Date("01/01/2000"), icon1 },
                { new Integer(2), "ni", Boolean.TRUE, new Date("04/15/1999"), icon2 },
                { new Integer(3), "san", Boolean.FALSE, new Date("12/07/1941"), icon2 },
                { new Integer(4), "shi", Boolean.TRUE, new Date("02/29/2000"), icon1 },
                { new Integer(5), "go", Boolean.FALSE, new Date("05/23/1995"), icon1 }, };

        String columnNames[] = { "English", "Japanese", "Boolean", "Date", "ImageIcon" };

        public int getColumnCount() {
            return columnNames.length;
        }// ww w  .ja va 2s .  co  m

        public String getColumnName(int column) {
            return columnNames[column];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column];
        }

        public Class getColumnClass(int column) {
            return (getValueAt(0, column).getClass());
        }

        public void setValueAt(Object value, int row, int column) {
            rowData[row][column] = value;
        }

        public boolean isCellEditable(int row, int column) {
            return (column != 4);
        }
    };

    JFrame frame = new JFrame("Column Renderer Table");
    JTable table = new JTable(model);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
}