开发者

How can I fill the height of JScrollPane with empty JTable Rows?

开发者 https://www.devze.com 2023-03-06 06:55 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号