How can I fill the height of JScrollPane with empty JTable Rows?
I am putting a JTable开发者_运维知识库 in a JScrollPane, but the table only contain several rows, which makes the rest of the space in the pane just empty.
Is there any way I can do to make the JTable fill the entire pane? say, put empty rows so that the pane will be filled.
Thanks
table.setFillsViewportHeight( true );
The JTable
method setPreferredScrollableViewportSize()
is another way to manage screen real estate. There's an example here.
I'm not sure if this is the best way, but it should work fine:
int rows = jTable1.getRowCount();
int rowHeight = jTable1.getRowHeight();
int tableHeight = jTable1.getTableHeader().getHeight()+(rows*rowHeight);
System.out.println("JScrollpane: "+jScrollPane1.getViewport().getHeight());
while( tableHeight<jScrollPane1.getViewport().getHeight()){
System.out.println("JTable: "+tableHeight);
((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{null,null,null,null});
tableHeight+=rowHeight;
}
Obviously, you'll need to change the model and the inserted data to whatever you're using.
精彩评论