JTable将无法在JScrollPane中滚动 [英] JTable won't scroll in JScrollPane

查看:103
本文介绍了JTable将无法在JScrollPane中滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使表格滚动时遇到了麻烦;只是不会.我查看了其他堆栈答案并尝试了它们,但是它们没有用;我不确定是否与这些解决方案有冲突.

I've been having trouble trying to get the table to scroll; it just won't. I've looked at other stack answers and tried them, but they aren't working; and I'm not sure if I have something conflicting with those solutions.

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中没有任何变化.我需要做些重新粉刷吗? -

So a window opens, which is the server program. Client programs connect to the server and when they do, a method is triggered in my table model class which adds a new row into the table. (I can see the row) Then at the end of that method it calls another but nothing changes in the ScrollPane. Do I need to do some kind of repainting? -

Server.updateTableScroll();

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);        
}

想法

因此,在Eclipse中,我使用Window Builder来制作GUI,以下for循环将使用滚动条显示表!但是当我在另一点运行相同的addClient()方法时,滚动条将不会出现.

So in Eclipse I use Window Builder to make the GUI, and the following for loop will display the table with the scrollbar! But when I run the same addClient() method at another point, then the scroll bar won't appear.

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();
            }
        }
    });
}

推荐答案

按照建议 pack()封闭的顶层容器.另外,

Instead of setBounds(), override getPreferredScrollableViewportSize(), as suggested here, and pack() the enclosing top-level container. Also, the API authors "recommend that you put the component in a JPanel and set the border on the JPanel."

附录:JTable侦听其TableModel时,请验证是否从模型中触发了正确的事件.如果您扩展 AbstractTableModel fireTableXxx()方法之一将是合适的.

Addendum: As a JTable listens to its TableModel, verify that the correct event is fired from the model. If you extend AbstractTableModel, one of the fireTableXxx() methods will be appropriate.

这篇关于JTable将无法在JScrollPane中滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆