How to set JTable on JFrame?

Viewed 57

This is my Java code. In this code I am trying to add JTable on JFrame but table does not show on frame what is the problem in my code?

    table_1 = new JTable();
    table_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
    table_1.setBorder(new LineBorder(new Color(0, 0, 0)));
    table_1.setModel(new DefaultTableModel(
        new Object[][] {
            },
        new String[] {
            "Last Name", "First Name"
            }
        ) {
            Class[] columnTypes = new Class[] {
                String.class, String.class
            };
            public Class getColumnClass(int columnIndex) {
                return columnTypes[columnIndex];
            }
        });
        table_1.setBounds(20, 314, 1254, -74);
        contentPane.add(table_1);
    
1 Answers

Hard to try the code, very incomplete, but I would guess that
table_1.setBounds(20, 314, 1254, -74);
with a negative height is very difficult to display.

It is better not to use the null layout manager1...
check Oracle's Laying Out Components Within a Container tutorial.

Also usual to add the JTable to a JScrollPane.

1 - assumption since missing the corresponding code and using setBounds

Related