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

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable t = new JTable(10, 1);
    frame.add(new JScrollPane(t));

    t.getSelectionModel().clearSelection();
    t.getSelectionModel().addSelectionInterval(5, 6);
    t.getSelectionModel().addSelectionInterval(8, 8);
    frame.pack();/* ww w.  j  a  v  a2 s .com*/
    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();

    JPanel container = new JPanel(new BorderLayout());

    // Add header in NORTH slot
    container.add(header, BorderLayout.NORTH);

    // Add table itself to CENTER slot
    container.add(table, BorderLayout.CENTER);
}

From source file:Main.java

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

    JTable table = new JTable(4, 5); // 4 rows & 5 columns

    table.setRowSelectionAllowed(false);
    table.setColumnSelectionAllowed(false);
    table.setCellSelectionEnabled(false);

    frame.add(new JScrollPane(table));

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

From source file:Main.java

public static void main(String[] args) {

    JTable table = new JTable(5, 5) {
        @Override/* w w w  .j a  va  2s . c  o m*/
        public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
            Component comp = super.prepareRenderer(renderer, row, col);
            ((JLabel) comp).setHorizontalAlignment(JLabel.RIGHT);
            return comp;
        }
    };
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    JScrollPane scrollPane = new JScrollPane(table);

    JFrame f = new JFrame();
    f.getContentPane().add(scrollPane);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Object[][] cellData = { { "1-1", "1-2" }, { "2-1", "2-2" } };
    String[] columnNames = { "col1", "col2" };

    JTable table = new JTable(cellData, columnNames);

    JFrame f = new JFrame();
    f.setSize(300, 300);/* w  w w .  j  av  a2 s  . co  m*/
    f.add(new JScrollPane(table));
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JTable(new Object[][] { { 1, 2, 3 }, { 4, 5, 6 } }, new Object[] { "one", "two", "three" }) {
        {/* w  w w  . j a  va 2  s. c  om*/
            addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    System.out.println("mouseDragged");
                }

                @Override
                public void mousePressed(MouseEvent e) {
                    System.out.println("mousePressed");
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    System.out.println("mouseReleased");
                }
            });
        }
    });
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable(5, 5);
    JTableHeader header = table.getTableHeader();
    header.addMouseListener(new ColumnHeaderListener());
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setSize(450, 250);/* w w  w  .  ja  v a  2s . c o  m*/

    JTable table = new JTable(5, 5);

    TableColumn testColumn = table.getColumnModel().getColumn(0);

    JComboBox<String> comboBox = new JComboBox<>();
    comboBox.addItem("This");
    comboBox.addItem("is");
    comboBox.addItem("a");
    comboBox.addItem("Sample program");
    testColumn.setCellEditor(new DefaultCellEditor(comboBox));

    frame.add(table);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    Integer[][] data = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    String[] cols = { "A", "B", "C" };

    JTable table = new JTable(data, cols);

    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.black);
    header.setForeground(Color.yellow);

    JOptionPane.showMessageDialog(null, new JScrollPane(table));
}

From source file:Main.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);

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    frame.setSize(300, 150);/*  www.  j a v  a 2  s.co  m*/
    frame.setVisible(true);

}