我一直在嘗試讓表格滾動時遇到問題;它不會。我查看了其他堆棧的答案並嘗試了它們,但它們不起作用;我不確定我是否與這些解決方案有衝突。JTable不會在JScrollPane中滾動
tableModel = new TableModel(); //Custom Table Model
table = new JTable();
table.setBorder(null);
table.setFillsViewportHeight(true);
table.setModel(tableModel);
JScrollPane tblScrollPane = new JScrollPane(table);
tblScrollPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
tblScrollPane.setBounds(245, 17, 560, 425);
frmServerAdministration.getContentPane().add(tblScrollPane);
編輯:更多信息
所以打開一個窗口,這是服務器程序。客戶端程序連接到服務器,當他們這樣做時,會在我的表模型類中觸發一個方法,該方法向表中添加一個新行。 (我可以看到該行)然後在該方法結束時調用另一個,但ScrollPane中沒有任何更改。我需要做一些重新粉刷嗎? -
Server.updateTableScroll();
public static void updateTableScroll() {
System.out.println("Here"); //NOTE: I do see this printed out
int last = table.getModel().getRowCount() - 1;
Rectangle r = table.getCellRect(last, 0, true);
table.scrollRectToVisible(r);
}
編輯2:思考
所以在Eclipse中我使用Window Builder創建圖形用戶界面,下面的for循環將顯示錶的滾動條!但是當我在另一個點上運行相同的addClient()方法時,滾動條不會出現。
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Server window = new Server();
window.frmServerAdministration.setVisible(true);
for(int i = 0; i < 20; i++){
tableModel.addClient(i, String.valueOf(i));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
這看起來很有用,但我不能真正遵循它。你能詳細說一下嗎? –
而且,哪個組件在JPanel 上? –
這是一個完整的[示例](http://stackoverflow.com/a/14429388/230513);如果沒有幫助,請修改您的問題以包含顯示您遇到的任何問題的[sscce](http://sscce.org/)。 – trashgod