Java JTable 滚动到指定的行索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/853020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:12:48  来源:igfitidea点击:

JTable Scrolling to a Specified Row Index

javaswingjtablejscrollpane

提问by Chris Dail

I have a JTable that is within a JScrollPane.Rows are added to the table at runtime based on events that happen in my application. I want to have the scoll pane scroll to the bottom of the table when a new row is added to the table.

我有一个位于JScrollPane.行内的 JTable,在运行时根据我的应用程序中发生的事件添加到表中。当向表中添加新行时,我想让 scoll 窗格滚动到表的底部。

For JLists There is the [ensureIndexIsVisible][1]()that forces a particular index in the list to be visible. I'm looking for the same thing but for a JTable. It looks like I might have to manually move the scrolling view on the scroll pane but I figured there had to be an easier way.

对于 JLists[ensureIndexIsVisible][1]()强制列表中的特定索引可见。我正在寻找相同的东西,但要寻找 JTable。看起来我可能需要手动移动滚动窗格上的滚动视图,但我认为必须有更简单的方法。

采纳答案by Pierre

See this example : http://www.exampledepot.com/egs/javax.swing.table/Vis.html

请参阅此示例:http: //www.exampledepot.com/egs/javax.swing.table/Vis.html

update: the link is now obsolete, here is the code (from http://smi-protege.stanford.edu/repos/protege/protege-core/trunk/src/edu/stanford/smi/protege/util/ComponentUtilities.java)

更新:链接现已过时,这里是代码(来自http://smi-protege.stanford.edu/repos/protege/protege-core/trunk/src/edu/stanford/smi/protege/util/ComponentUtilities.爪哇

public static void scrollToVisible(JTable table, int rowIndex, int vColIndex) {
        if (!(table.getParent() instanceof JViewport)) {
            return;
        }
        JViewport viewport = (JViewport)table.getParent();

        // This rectangle is relative to the table where the
        // northwest corner of cell (0,0) is always (0,0).
        Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);

        // The location of the viewport relative to the table
        Point pt = viewport.getViewPosition();

        // Translate the cell location so that it is relative
        // to the view, assuming the northwest corner of the
        // view is (0,0)
        rect.setLocation(rect.x-pt.x, rect.y-pt.y);

        table.scrollRectToVisible(rect);

        // Scroll the area into view
        //viewport.scrollRectToVisible(rect);
    }

回答by Valentin Rocher

JList internally use scrollRectToVisibleand specify the coordinates to scroll to. I think you will have to recode a similar functionality for JTable.

JList 内部使用scrollRectToVisible并指定要滚动到的坐标。我认为您将不得不为 JTable 重新编码类似的功能。

回答by martinusadyh

It's very easy, JTable has scrollRectToVisible method too. If you want, you can try something like this to make scrollpane go to to the bottom if a new record is added :

很简单,JTable 也有 scrollRectToVisible 方法。如果你愿意,你可以尝试这样的事情,如果添加了新记录,使滚动窗格转到底部:

jTable1.getSelectionModel().setSelectionInterval(i, i);
jTable1.scrollRectToVisible(new Rectangle(jTable1.getCellRect(i, 0, true)));

Where iis last added record.

最后加入记录。

回答by MGaidamak

The first answer works well, but the selected row gets positioned at the bottom of the table. So I created this modified version:

第一个答案效果很好,但所选行位于表格底部。所以我创建了这个修改版本:

private void scrollToVisible(int rowIndex, int vColIndex ) {
        JTable table = getTablePanel().getTable();
        if (!(table.getParent() instanceof JViewport)) {
            return;
        }
        if (table.getRowCount()<1){
            return;
        }
        JViewport viewport = (JViewport)table.getParent();
        // view dimension
        Dimension dim = viewport.getExtentSize();
        // cell dimension
        Dimension dimOne = new Dimension(0,0);

        // This rectangle is relative to the table where the
        // northwest corner of cell (0,0) is always (0,0).
        Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
        Rectangle rectOne;
        if (rowIndex+1<table.getRowCount()) {
            if (vColIndex+1<table.getColumnCount())
                vColIndex++;
            rectOne = table.getCellRect(rowIndex+1, vColIndex, true);
            dimOne.width=rectOne.x-rect.x;
            dimOne.height=rectOne.y-rect.y;
        }

        // '+ veiw dimension - cell dimension' to set first selected row on the top

        rect.setLocation(rect.x+dim.width-dimOne.width, rect.y+dim.height-dimOne.height);

        table.scrollRectToVisible(rect);
    }

Now the selected row gets positioned at the top of the table.

现在选定的行被定位在表格的顶部。

回答by user3015208

It seems to me a lot easier to set the viewport position instead of scrolling the table. Following is my code.

在我看来,设置视口位置而不是滚动表格要容易得多。以下是我的代码。

public void scrollCellToView(int rowIndex, int vColIndex) {
    if (!(this.getParent() instanceof JViewport)) {
        return;
    }
    JViewport viewport = (JViewport) this.getParent();
    Rectangle rect = this.getCellRect(rowIndex, vColIndex, true);
    Rectangle viewRect = viewport.getViewRect();

    int x = viewRect.x;
    int y = viewRect.y;

    if (rect.x >= viewRect.x && rect.x <= (viewRect.x + viewRect.width - rect.width)){

    } else if (rect.x < viewRect.x){
        x = rect.x;
    } else if (rect.x > (viewRect.x + viewRect.width - rect.width)) {
        x = rect.x - viewRect.width + rect.width;
    }

    if (rect.y >= viewRect.y && rect.y <= (viewRect.y + viewRect.height - rect.height)){

    } else if (rect.y < viewRect.y){
        y = rect.y;
    } else if (rect.y > (viewRect.y + viewRect.height - rect.height)){
        y = rect.y - viewRect.height + rect.height;
    }

    viewport.setViewPosition(new Point(x,y));
}