我一直在尝试让表格滚动时遇到问题;它不会。我查看了其他堆栈的答案并尝试了它们,但它们不起作用;我不确定我是否与这些解决方案有冲突。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