JTable 转到行

发布于 2024-10-10 18:15:07 字数 519 浏览 1 评论 0原文

我正在尝试实现 goto 函数。我有一个包含几千行的 JTable,我希望能够跳转到指定的行号。

else if (BUTTON_GOTO.equals(command))
{
    int gotoLine = Integer.valueOf(gotoField.getText());            
    logTable.setRowSelectionInterval(gotoLine, gotoLine);
}

上面的代码将突出显示我正在查找的行,但不会跳转到它。有谁知道该怎么做?

谢谢

编辑 使用下面的解决方案时存在一个错误,应用程序会跳转到比所需行短的几行。有关更多详细信息,请参阅以下主题:

Stack Overflow - Java JTable Goto Row Bug

I am trying to implement a goto function. I have a JTable that has a few thousand rows and I want to be able to jump to a specified row number.

else if (BUTTON_GOTO.equals(command))
{
    int gotoLine = Integer.valueOf(gotoField.getText());            
    logTable.setRowSelectionInterval(gotoLine, gotoLine);
}

The code above will highlight the row I am looking for, but does not jump to it. Does anyone know how to do this?

Thanks

EDIT
There is a bug using the solution below where the application jumps a few lines short of the desired line. See the topic below for further details:

Stack Overflow - Java JTable Goto Row Bug

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

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

发布评论

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

评论(2

懵少女 2024-10-17 18:15:07

尝试:

logTable.scrollRectToVisible(logTable.getCellRect(gotoLine, 0, true));

getCellRect() 返回一个将单元格限制在给定行和列的 矩形,并且 scrollRectToVisible() 告诉表的父级(如果您使用的是 JScrollPane,则应该是 JViewPort ) 滚动到那里。

Try:

logTable.scrollRectToVisible(logTable.getCellRect(gotoLine, 0, true));

getCellRect() returns a Rectangle which bounds the cell at the given row and column, and scrollRectToVisible() tells the table's parent (which should be a JViewPort if you are using a JScrollPane) to scroll there.

天煞孤星 2024-10-17 18:15:07

我有一个类似的问题,我在上面的帮助下解决了
首先我添加了 gotoButton 和文本字段

    JPanel gotoPanel = new JPanel();
    JButton gotoRowButton = new JButton("Goto Address");
    JTextField gotoAddressTextField = new JTextField();
    gotoAddressTextField.setColumns(12);
    gotoPanel.add(gotoRowButton);
    gotoPanel.add(gotoAddressTextField);

    frame.add(gotoPanel, BorderLayout.NORTH);

,然后我为 gotoRowButton 添加了一个 actionListnener

        gotoRowButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
            
             table.setRowSelectionInterval(
                 Integer.parseInt(gotoAddressTextField.getText(), 
                 Integer.parseInt(gotoAddressTextField.getText()));
            table.scrollRectToVisible(
            table.getCellRect(
              Integer.parseInt(gotoAddressTextField.getText()), 1, true)
      );
        }
    });

这是我工作的视觉效果
显示 gotobutton 和文本字段以及 gotoRow 突出显示的图像

I have had a similar issue which i solved with help of above
first I added gotoButton and text field

    JPanel gotoPanel = new JPanel();
    JButton gotoRowButton = new JButton("Goto Address");
    JTextField gotoAddressTextField = new JTextField();
    gotoAddressTextField.setColumns(12);
    gotoPanel.add(gotoRowButton);
    gotoPanel.add(gotoAddressTextField);

    frame.add(gotoPanel, BorderLayout.NORTH);

then I added an actionListnener for the gotoRowButton

        gotoRowButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
            
             table.setRowSelectionInterval(
                 Integer.parseInt(gotoAddressTextField.getText(), 
                 Integer.parseInt(gotoAddressTextField.getText()));
            table.scrollRectToVisible(
            table.getCellRect(
              Integer.parseInt(gotoAddressTextField.getText()), 1, true)
      );
        }
    });

a visual of my work
An image showing gotobutton and textfield and highlight of gotoRow

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