2012-02-06 91 views
1

我想水平滚动条添加到我的JScrollPane的,因为我的表,现在看起来像下面这样:如何水平/垂直滚动条添加到JScrollPane中

enter image description here

这里是我的代码来创建表:

this.table = new JTable(); 
    this.table.setShowGrid(false); 
    this.table.getTableHeader().setFont(ReportViewConstants.TABLE_FONT); 
    this.table.setFont(ReportViewConstants.TABLE_FONT); 

    this.scrollPane = new JScrollPane(this.table); 
    Dimension size = new Dimension(300, 400); 
    this.scrollPane.setPreferredSize(size); 
    this.scrollPane.setMinimumSize(size); 
    this.scrollPane.getViewport().setBackground(Color.WHITE); 

你能指出我做错了什么吗?当我改变了行创建一个JScrollPane到:

this.scrollPane = new JScrollPane(this.table, 
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

滚动条是有的,但我还是不能水平滚动。请参阅下面的截图。

enter image description here

谢谢!

回答

-1

尝试

Dimension tableSize = new Dimension(500, 400); 
this.table.setPreferredSize(tableSize); 
+1

-1,通常你不应该在任何组件上使用setPreferred大小。您绝对不应该设置添加到滚动窗格的组件的首选大小,因为滚动条不会在您添加/删除表中的行/列时动态显示。 – camickr 2012-02-06 18:19:18