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(final Object[][] rowData, final Object[] columnNames) 

Source Link

Document

Constructs a JTable to display the values in the two dimensional array, rowData, with column names, columnNames.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    Object rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    Object headers[] = { "Upper", "Lower" };
    JTable table = new JTable(rows, headers);
    table.setTableHeader(null);/*from w w  w.j  av  a 2 s  . c  o  m*/
    JScrollPane scrollPane = new JScrollPane(table);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int rows = 10;
    int cols = 5;
    JTable table = new JTable(rows, cols);
    JTableHeader header = table.getTableHeader();

    ColumnHeaderToolTips tips = new ColumnHeaderToolTips();
    for (int c = 0; c < table.getColumnCount(); c++) {
        TableColumn col = table.getColumnModel().getColumn(c);
        tips.setToolTip(col, "Col " + c);
    }/*from   w w w  .j  a v a  2  s .c  om*/
    header.addMouseMotionListener(tips);
}

From source file:ResizeColumnWidth.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 JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

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

    frame.add(scrollPane, BorderLayout.CENTER);

    TableColumn column = null;/*w w w .j  a v  a  2s.  c  o  m*/
    for (int i = 0; i < 3; i++) {
        column = table.getColumnModel().getColumn(i);
        if (i == 2) {
            column.setPreferredWidth(100); //sport column is bigger
        } else {
            column.setPreferredWidth(50);
        }
    }

    frame.setSize(300, 150);
    frame.setVisible(true);

}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame f = new JFrame("JTable Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object rows[][] = { { "A", "Name 1", "38.94" }, { "B", "Name 2", "7.70" }, { "C", "Name 3", "112.65" } };

    Object columns[] = { "Symbol", "Name", "Price" };
    JTable table = new JTable(rows, columns);
    JScrollPane scrollPane = new JScrollPane(table);
    f.add(scrollPane, BorderLayout.CENTER);
    f.setSize(300, 200);//from   www.  j  av a 2  s  . co  m
    f.setVisible(true);
}

From source file:TableModelListenerDemo.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 JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);
    table.getModel().addTableModelListener(new TableModelListener() {

        public void tableChanged(TableModelEvent e) {
            System.out.println(e);
        }//from w w w.ja  v  a2 s. c om
    });

    table.setValueAt("", 0, 0);
    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(scrollPane, 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);

    Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
            { "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
    Object columnNames[] = { "1", "2", "3" };
    JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    table.setTableHeader(null);/*from  w w w.  j av a2 s. co  m*/

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String[] columns = { "Name", "Age" };

    Object[][] content = { { "R", new Integer(24) }, { "A", new Integer(25) }, { "J", new Integer(30) },
            { "A", new Integer(32) }, { "S", new Integer(27) } };

    JTable table = new JTable(content, columns);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel jPanel = new JPanel(new GridLayout(2, 0));
    jPanel.setOpaque(true);//from   w ww  .j a va 2  s.  co m
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    jPanel.add(new JScrollPane(table));
    /* Add the panel to the JFrame */
    frame.add(jPanel);
    /* Display the JFrame window */
    frame.pack();
    frame.setVisible(true);

    table.print();
}

From source file:EmptyColumnHeader.java

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

    Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
            { "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
    Object columnNames[] = { "", "", "" };
    JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);/*w  w  w.  ja  va2 s  .  co m*/
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] argv) {
    Vector rowData = new Vector();
    for (int i = 0; i < 1; i++) {
        Vector colData = new Vector(Arrays.asList("qq"));
        rowData.add(colData);/* w ww  . jav a  2  s . c  o m*/
    }

    String[] columnNames = { "a" };

    Vector columnNamesV = new Vector(Arrays.asList(columnNames));

    JTable table = new JTable(rowData, columnNamesV);
    JFrame f = new JFrame();
    f.setSize(300, 300);
    f.add(new JScrollPane(table));
    f.setVisible(true);
}

From source file:ResizeTable.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 JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column", "Resize Off",
            "Resize Subsequent Columns" };

    final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN,
            JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF, JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS };

    JComboBox resizeModeComboBox = new JComboBox(modes);

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            JComboBox source = (JComboBox) e.getSource();
            int index = source.getSelectedIndex();
            table.setAutoResizeMode(modeKey[index]);
        }/*from ww w . j  av  a 2  s . c om*/
    };
    resizeModeComboBox.addItemListener(itemListener);

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

    frame.add(resizeModeComboBox, BorderLayout.NORTH);
    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);

}