Swing GUI:当 JTable 变大时滚动不会更新

发布于 2024-07-14 10:23:33 字数 569 浏览 7 评论 0原文

我有一个 Java Swing GUI,但在 JScrollPane 中的 JTable 遇到了问题。 由于某种原因,当程序执行期间表模型的行数增加时,JScrollPane 不会更新 - 也就是说,如果行数增加使得表的高度超过了滚动视图的高度,滚动窗格没有按预期更新。 (新行按预期显示在屏幕上)。 如果调整窗口大小,滚动会按预期更新。

垂直滚动策略是VERTICAL_SCROLLBAR_​​AS_NEEDED,表模型fireTableDataChanged被调用。

不幸的是代码有点复杂,所以我无法提供导致问题的代码示例。 但想问问是否有人有一些直接的想法。

编辑:还有一点更令人困惑:水平滚动策略是HORIZONTAL_SCROLLBAR_​​AS_NEEDED,并且表格宽度是否超​​过视图宽度(即使用水平滚动条),不会发生此问题...

编辑2:问题不在于表格应该滚动,而是滚动条未激活,因为它们应该。

I got a Java Swing GUI and got a problem with a JTable in a JScrollPane. For some reason, when the rows of the table model are increased during the program execution, the JScrollPane isn't updated - that is, if the rows are increased so that the height of the table is over the height of the scroll view, the scroll panes aren't updated as supposed. (The new rows are shown at the screen as expected). If the window is resized, scrolling is updated as expected.

The vertical scrolling policy is VERTICAL_SCROLLBAR_AS_NEEDED, table models fireTableDataChanged is called..

Unfortunately the code's a bit complex so I can't provide an code sample causing the problem. But thought to ask if somebody got some ideas straight..

EDIT: Still a bit more confusing : horizontal scrolling policy is HORIZONTAL_SCROLLBAR_AS_NEEDED, and if the table width if over the view width (that is, the horizontal scrollbar is used), this problem doesn't occur...

EDIT2: The problem isn't that the table should be scrolled but that the scrollbar's aren't activated as they should.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箜明 2024-07-21 10:23:33

您可能需要发布一些代码。 我刚刚完成了以下测试,它的工作原理如广告所示,即当行数超过视口高度时激活垂直滚动条:

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.AbstractTableModel;

public class JTableTest {

    public static void main(String[] args) {
        final MyTableModel tm = new MyTableModel();
        tm.addData(new Data("R1C1", "R1C2"));

        JTable table = new JTable(tm);
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout());
        frame.add(scrollPane);
        frame.pack();
        frame.setSize(400, 150);
        frame.setVisible(true);

        Thread t = new Thread(new Runnable() {
            private int count = 2;
            public void run() {
                for ( ; ; ) {
                    tm.addData(new Data("R" + count + "C1", "R" + count + "C2"));
                    count++;
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
    }

    private static class MyTableModel extends AbstractTableModel {
        private List<Data> dataList = new ArrayList<Data>();

        public int getColumnCount() {
            return 2;
        }

        public void addData(Data data) {
            dataList.add(data);
            fireTableRowsInserted(dataList.size()-1, dataList.size()-1);
        }

        public int getRowCount() {
            return dataList.size();
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            Data data = dataList.get(rowIndex);
            return columnIndex == 0 ? data.data1 : data.data2;
        }
    }

    private static class Data {
        public String data1;
        public String data2;

        public Data(String data1, String data2) {
            this.data1 = data1;
            this.data2 = data2;
        }
    }
}

You might need to post some of your code. I've just knocked up the following test and it works as advertised, i.e. vertical scrollbars are activated when the number of rows exceeds the viewport height:

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.AbstractTableModel;

public class JTableTest {

    public static void main(String[] args) {
        final MyTableModel tm = new MyTableModel();
        tm.addData(new Data("R1C1", "R1C2"));

        JTable table = new JTable(tm);
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout());
        frame.add(scrollPane);
        frame.pack();
        frame.setSize(400, 150);
        frame.setVisible(true);

        Thread t = new Thread(new Runnable() {
            private int count = 2;
            public void run() {
                for ( ; ; ) {
                    tm.addData(new Data("R" + count + "C1", "R" + count + "C2"));
                    count++;
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
    }

    private static class MyTableModel extends AbstractTableModel {
        private List<Data> dataList = new ArrayList<Data>();

        public int getColumnCount() {
            return 2;
        }

        public void addData(Data data) {
            dataList.add(data);
            fireTableRowsInserted(dataList.size()-1, dataList.size()-1);
        }

        public int getRowCount() {
            return dataList.size();
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            Data data = dataList.get(rowIndex);
            return columnIndex == 0 ? data.data1 : data.data2;
        }
    }

    private static class Data {
        public String data1;
        public String data2;

        public Data(String data1, String data2) {
            this.data1 = data1;
            this.data2 = data2;
        }
    }
}
浪菊怪哟 2024-07-21 10:23:33

嗯..返回问题后,我发现 JScrollPane 中的行标题自定义导致了问题。 (一些首选尺寸设置为不太合理的值等)..

Hmm.. after returning to the issue I found out that our row header customizing in JScrollPane was causing the problem. (Some preferredsizes were set with not-so-sensible values etc)..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文