0

I have a JTable with fixed size that I want to load all my data on.

table.setPreferredSize(1400, 600);
DefaultTableModel model = (DefaultTableModel) table.getModel();
while(resultSet.next()){        
   model.addRow(row);
}

But when I scroll down the table, not all the rows are visible. Rows that are greater than the height of JTable are out of view.

I tried to set the Height of table to 3000 and data becomes visible. But I only want my table to have a fixed height and use scroll to navigate through the rest of data that are out of view from the table.

How can I dynamically set the Height of the JTable based on number of rows inserted to the table model?

1
  • 1
    override setPreferredScrollableViewportSize instead of setPreferredSize (hasn't nothing with your question, is about design and correct method),
    – mKorbel
    May 9, 2015 at 4:51

1 Answer 1

0

Add the table in the scroll pane, it will allow to scroll through the table contents as needed. Add this scroll pane to your JPanel that is being displayed.

    JScrollPane scrollPane = new JScrollPane(table);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.