Java jTable is not visible inside jScrollPane

2.3k views Asked by At

Hello I'm having a problem displaying my JTable inside a JScrollPane.

This is my code:

table = new JTable();
    table.setBounds(16, 203, 362, 16);
    //getContentPane().add(table);
    table.setShowHorizontalLines(true);
    table.setShowVerticalLines(true);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setModel(new DefaultTableModel(
        new Object[][] {
            {"2", "DN Korina", "200"},
        },
        new String[] {
            "QTY", "Item Code", "Amount"
        }
    ));
    table.getColumnModel().getColumn(0).setPreferredWidth(105);
    table.getColumnModel().getColumn(1).setPreferredWidth(244);
    table.getColumnModel().getColumn(2).setPreferredWidth(220);
    table.setBackground(Color.PINK);
    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.yellow);
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBounds(16, 480, 359, -240);
    getContentPane().add(scrollPane);

I'm using WindowBuilder Pro and I can't see the jScrollPane in the design view. If I removed JScrollPane and let JTable alone, JTable is visible. But I can't see the header. I've read that to view JTable's header, JTable must be placed inside JScrollPane. Any ideas why I can't see my JScrollPane and JTable? Thanks.

1

There are 1 answers

4
Dunkey On BEST ANSWER

Finally, I got it to work!

I set the same bounds for JTable and JScrollPane.

table = new JTable();
table.setBounds(16, 203, 362, 16);


scrollPane.setBounds(16, 203, 362, 266);