How can I auto resize Jtable's height to fit parent container when there aren't enough rows for scrollbar to show.
I think you are looking for:
table. setFillsViewportHeight( true );
Edit:
but doesn't seem to work...
Add the following lines of code to your example:
contentPane.setBackground(Color.RED);
scrollPane.getViewport().setBackground(Color.BLUE);
and you will see a red border.
Then remove:
table. setFillsViewportHeight( true );
and you will see red and blue, indicating that the table height was originally increased.
If you are expecting to see empty cells painted then it won't work. The table can only paint what is in the model. If you want more rows of data painted, then you need to add more rows of data to the model. And if the height decreases you need to remove data from the model.
Of course now you need to track which is "valid" rows of data and which is "filler" rows of data. So you would need to create a custom TableModel to manage this. You would then add a ComponentListener to the table to handle the height changes in the table and then do your manipulation of the TableModel.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…