I am trying to add a JScrollPane to my JTable, but it doesn’t seem to works. I have a JTable with 21 rows and 5 columns, and I’m adding a JScrollPane as per the following code…
public Targy_felv() {
JScrollPane scrollPane;
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 300);
table = new JTable();
Object o[] = new Object[]{"Tárgynév", "Oktató", "Kredit", "Félév", "Tárgykód"};
table.setModel(new DefaultTableModel(get_Tárgyak(), o));
scrollPane = new JScrollPane();
scrollPane.getViewport().add(table);
frame.add(table);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Could someone please help me to understand why the scrollbars aren’t appearing.
Make sure you’re adding the
JScrollPaneto yourJFrame, not theJTable. If you originally just had aJFrameand aJTableyou would have added it like this…If you’re adding the
JScrollPane, you need to change youradd()method to add theJScrollPaneinstead of theJTable, either like this…or like this, if you need to reference the
JScrollPanelater in your code…