Swing JTable:当行可见或滚动到底部时发生事件? [英] Swing JTable: Event when row is visible, or when scrolled to the bottom?

查看:67
本文介绍了Swing JTable:当行可见或滚动到底部时发生事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,当JTable滚动到某个特定的行变得可见时,通知该行,否则,当表格的底部滚动到视图中时,通知失败.理想情况下,应在不进行轮询的情况下进行此操作,而应通过事件触发.有什么想法吗?

I'm looking for a way to be informed when a JTable has scrolled such that a particular row becomes visible, or failing that, when the bottom of the table has scrolled into view. Ideally this should be done without polling, but through some event firing. any ideas?

推荐答案

向滚动窗格的视口添加ChangeListener.

Add a ChangeListener to the viewport of the scrollpane.

    viewport = scrollpane.getViewport();
    viewport.addChangeListener(this);

然后检查可见行(也可以轻松扩展到列)

then this checks the visible rows (can easily be extended to columns as well)

public void stateChanged(ChangeEvent e)
{
    Rectangle viewRect = viewport.getViewRect();
    int first = table.rowAtPoint(new Point(0, viewRect.y));
    if (first == -1)
    {
        return; // Table is empty
    }
    int last = table.rowAtPoint(new Point(0, viewRect.y + viewRect.height - 1));
    if (last == -1)
    {
        last = tableModel.getRowCount() - 1; // Handle empty space below last row
    }

    for (int i = first; i <= last; i++)
    {
        int row = sorter.convertRowIndexToModel(i); // or: row = i
        //... Do stuff with each visible row
    }

    if (last == tableModel.getRowCount() - 1) {} //... Last row is visible
}

如果表不可排序,请忽略sorter.

Ignore the sorter if your table is not sortable.

这篇关于Swing JTable:当行可见或滚动到底部时发生事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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