使用 JTable 和 JScroll 窗格进行滚动锁定

发布于 2024-12-04 17:54:40 字数 506 浏览 1 评论 0原文

我有一个使用 EventTableModel 并且位于 JScrollPane 中。 EventTableModel 从事件列表中获取实时更新并将结果显示在表中。当新结果进入表中时,新的信息将显示在表的顶部。

但是,我想要做的是冻结表格以显示当我按下名为“锁定表格”的按钮时当前显示的内容。此按钮应与 Eclipse 控制台“Scroll Lock”具有相同的效果,因此当新项目出现时,当前项目应保留在屏幕上,并且不会在新项目出现时被推开。但新项目仍应添加,只是不会自动滚动到。

有谁知道我如何尝试实现此功能。这样,当更新到来时,表上的数据不会被迫离开屏幕,因此当按下复选框时,焦点仍保留在当前数据上。

感谢您的任何帮助。 迈克尔

I have a JTable which is created with the use of an EventTableModel and is in a JScrollPane. The EventTableModel takes live updates from an eventList and displays the result in the table. As new results come into the table and the new piece of information is displayed at the top of the table.

However, what I want to be able to do is freeze the table to show what is currently displayed when I press a button called 'Lock Table'. This button should have the same effect as the eclipse console 'Scroll Lock', therefore as new items appear the current items should remain on the screen and not be pushed off as new items appear. But new items should still be added just not automatically scrolled to.

Does anyone know how I can try achieve this functionality. So that as update come in, the data that is on the table is not forced off screen, therefore focuses remain on the current data when the check box is pressed.

Thanks for any help.
Michael

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

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

发布评论

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

评论(1

旧竹 2024-12-11 17:54:40

基本过程(用于在当前显示区域上方插入)

  • 在启用锁定时在表模型上安装 TableModelListener
  • :在锁定时记录接收插入时当前可见矩形下方的行数
  • ,滚动以使下方的行数保持

恒定工作代码(使用 JXTable,因为它有方便的滚动方法,对于核心表,只需自己进行计算:-)

public static class ScrollLock {
    private JXTable table;
    private boolean blocked;
    private int rowsBelow;

    public ScrollLock(JXTable table) {
        this.table = table;
        table.getModel().addTableModelListener(getTableModelListener());
    }

    private TableModelListener getTableModelListener() {
        TableModelListener l = new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                if (!blocked) return;
                if (e.getType() == TableModelEvent.INSERT) {
                    updateInsert(e.getFirstRow(), e.getLastRow());
                }
            }

        };
        return l;
    }

    protected void updateInsert(int firstRow, int lastRow) {
        // PENDING: assumption is that insert always above
        // need additional logic for other cases
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Rectangle r = table.getVisibleRect();
                int row =table.rowAtPoint(new Point(0, r.y + r.height));
                int lastVisible = table.getRowCount() - rowsBelow;
                table.scrollRowToVisible(lastVisible);
            }
        });
    }

    public void block() {
        Rectangle viewRect = table.getVisibleRect();
        int lastVisibleRow = table.rowAtPoint(new Point(0, viewRect.y + viewRect.height));
        rowsBelow = table.getRowCount() - lastVisibleRow;
        blocked = true;
    }

    public void unblock() {
        blocked = false;
        rowsBelow = -1;
    }

}

Basic procedure (for inserting above the current display area)

  • install a TableModelListener on the table's model
  • on enable lock: note the number of rows below the current visible rectangle
  • on receiving inserts while locked, scroll so that the number of rows below are kept constant

some working code (using JXTable, as it has convenience method for scrolling, for a core table simply do the calculations yourself :-)

public static class ScrollLock {
    private JXTable table;
    private boolean blocked;
    private int rowsBelow;

    public ScrollLock(JXTable table) {
        this.table = table;
        table.getModel().addTableModelListener(getTableModelListener());
    }

    private TableModelListener getTableModelListener() {
        TableModelListener l = new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                if (!blocked) return;
                if (e.getType() == TableModelEvent.INSERT) {
                    updateInsert(e.getFirstRow(), e.getLastRow());
                }
            }

        };
        return l;
    }

    protected void updateInsert(int firstRow, int lastRow) {
        // PENDING: assumption is that insert always above
        // need additional logic for other cases
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Rectangle r = table.getVisibleRect();
                int row =table.rowAtPoint(new Point(0, r.y + r.height));
                int lastVisible = table.getRowCount() - rowsBelow;
                table.scrollRowToVisible(lastVisible);
            }
        });
    }

    public void block() {
        Rectangle viewRect = table.getVisibleRect();
        int lastVisibleRow = table.rowAtPoint(new Point(0, viewRect.y + viewRect.height));
        rowsBelow = table.getRowCount() - lastVisibleRow;
        blocked = true;
    }

    public void unblock() {
        blocked = false;
        rowsBelow = -1;
    }

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