2012-02-06 92 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