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

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String[] columnTitles = { "First Name", "Last Name", "Weight (lb)", "Blood Group", "Age>20yrs" };
    Object[][] dataEntries = { { "Saravan", "Pantham", new Integer(50), "B", new Boolean(false) },
            { "Eric", "", new Integer(180), "O", new Boolean(true) },
            { "John", "", new Integer(120), "AB", new Boolean(false) },
            { "Mathew", "", new Integer(140), "A", new Boolean(true) }, };
    TableModel model = new EditableTableModel(columnTitles, dataEntries);
    JTable table = new JTable(model);
    table.createDefaultColumnsFromModel();

    String[] bloodGroups = { "A", "B", "AB", "O" };
    JComboBox comboBox = new JComboBox(bloodGroups);
    table.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(comboBox));

    frame.add(new JScrollPane(table));

    frame.setSize(300, 200);/*  w  w  w .  j av  a  2s .c  o m*/
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(final String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" } };
    final String headers[] = { "English", "Digit" };

    TableModel fixedColumnModel = new AbstractTableModel() {
        public int getColumnCount() {
            return 2;
        }/*from w  w w .j  a  va 2s  . c  o m*/

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

        public int getRowCount() {
            return 3;
        }

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

    JFrame frame = new JFrame("Scrollless Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(fixedColumnModel);

    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    frame.add(new JScrollPane(table), BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.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;//from ww w.  j  ava2 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);
}

From source file:MainClass.java

public static void main(final String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" } };
    final String headers[] = { "English", "Digit" };

    TableModel fixedColumnModel = new AbstractTableModel() {
        public int getColumnCount() {
            return 2;
        }/*from   w  w  w  .  j a v  a  2  s  .c o m*/

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

        public int getRowCount() {
            return 3;
        }

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

    JFrame frame = new JFrame("Scrollless Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(fixedColumnModel);

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

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

From source file:MainClass.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(2);

    ComboTableCellRenderer renderer = new ComboTableCellRenderer();
    column.setCellRenderer(renderer);//from w ww  .  j  a  v  a  2  s . c o  m

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

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

From source file:FileTableDemo.java

public static void main(String[] args) {
    // Figure out what directory to display;
    File dir;/* ww w .java2 s.c om*/
    if (args.length > 0)
        dir = new File(args[0]);
    else
        dir = new File(System.getProperty("user.home"));

    // Create a TableModel object to represent the contents of the directory
    FileTableModel model = new FileTableModel(dir);

    // Create a JTable and tell it to display our model
    JTable table = new JTable(model);

    // Display it all in a scrolling window and make the window appear
    JFrame frame = new JFrame("FileTableDemo");
    frame.getContentPane().add(new JScrollPane(table), "Center");
    frame.setSize(600, 400);
    frame.setVisible(true);
}

From source file:RowSorterDemo.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Sort Table Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object rows[][] = { { "J", 23 }, { "R", 24, }, { "E", 21, }, { "B", 27, }, { "A", 25, }, { "S", 22, }, };

    String columns[] = { "Name", "Age" };

    TableModel model = new DefaultTableModel(rows, columns) {
        public Class getColumnClass(int column) {
            Class returnValue;/* ww  w.  j  a  v a2  s.co 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);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    TableModel model = new ColorTableModel();
    JTable table = new JTable(model);

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

    ComboTableCellRenderer renderer = new ComboTableCellRenderer();
    column.setCellRenderer(renderer);//from   w  w  w  .  j ava 2s.c  o  m

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

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

From source file:RowSorterDemo.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Sort Table Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object rows[][] = { { "J", 23 }, { "R", 24, }, { "E", 21, }, { "B", 27, }, { "A", 25, }, { "S", 22, }, };
    /* Specify column names */
    String columns[] = { "Name", "Age" };
    /* Create a TableModel */
    TableModel model = new DefaultTableModel(rows, columns) {
        public Class getColumnClass(int column) {
            Class returnValue;//from   ww w. j av a2s.  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);
}

From source file:AbstractSample.java

public static void main(String args[]) {
    TableModel model = new AbstractTableModel() {
        Object rowData[][] = { { "one", "ichi" }, { "two", "ni" }, { "three", "san" }, { "four", "shi" },
                { "five", "go" }, { "six", "roku" }, { "seven", "shichi" }, { "eight", "hachi" },
                { "nine", "kyu" }, { "ten", "ju" } };

        Object columnNames[] = { "English", "Japanese" };

        public String getColumnName(int column) {
            return columnNames[column].toString();
        }/*from w w  w .j a va2 s  .  co  m*/

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

        public int getColumnCount() {
            return columnNames.length;
        }

        public Object getValueAt(int row, int col) {
            return rowData[row][col];
        }
    };
    JFrame frame = new JFrame("Abstract Sample");
    JTable table = new JTable(model);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}