Java JTable转到行Bug [英] Java JTable Goto Row Bug

查看:161
本文介绍了Java JTable转到行Bug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查找功能,在一个JTable中定位了一个字符串,有几千个条目。 迈克尔·迈耶斯(Michael Meyers)非常友好地帮助我完成了这个功能的goto部分。虽然...似乎有一个错误...

当用户搜索字符串时,应用程序正确地在JTable中找到该行并将其突出显示。它也试图把重点放在它,但并不总是。有时它会跳过我所寻找的线以上10多条线,我需要向下滚动才能看到它。正如我所说,在这个JTable中有几千个条目,如果我正在寻找一些东西,那么很难滚动。 是否可以将选定的条目集中在可见区域的中心?

  if .get(i).getLine()。contains(findStr))
{
logTable.scrollRectToVisible(logTable.getCellRect(thisPos,1,true)); // goto
logTable.setRowSelectionInterval(thisPos,thisPos); //突出显示
}

我不确定它是否有帮助, JTable设置代码:

  JTable logTable = new JTable(logTableModel); 
logTable.setShowGrid(true);
logTable.setShowVerticalLines(true);
logTable.setShowHorizo​​ntalLines(false);
logTable.setRowSorter(sorter);
logTable.getSelectionModel()。addListSelectionListener(new LogRowListener());

JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport()。add(logTable);
scrollPane.setPreferredSize(new Dimension(800,450));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_​​AS_NEEDED);

谢谢

编辑
以下是下载.jar文件的链接。该文件是说明问题的代码的有限版本。这个版本似乎一直跳2-3行,这并不总是在完整版本的情况下。



Demo.jar



这个演示代码甚至还有几百行,所以下面是我认为相关的部分。

  public class Proto extends JFrame implements ActionListener 
{
public Proto(){...}

@Override
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();

if(BUTTON_NEXT_FIND.equals(command))
{
findNext();




$ b private void findNext()
{
String findStr = findField.getText( );

int pos = selectedLogRow;

//如果我们再次搜索相同的字符串,则再前进一次
if(pos == lastFoundPos)
++ pos;

//在日志中搜索字符串
(pos< logs.size())
{
if(logs.get(pos))。 getLine()。contains(findStr))
{
logTable.scrollRectToVisible(logTable.getCellRect(pos,1,true));
logTable.setRowSelectionInterval(pos,pos);
lastFoundPos = pos;
break;
}
++ pos;





code $

有时它会跳过10行以上的行,我需要向下滚动以查看它。

>

发布您的 SSCCE 来证明问题。您可以创建一个包含一千行的表格,然后使用setValueAt(...)方法用您正在搜索的文本填充一些随机的单元格。


是否可以将所选条目聚焦在可见区域的中心?

您需要调整你想滚动到的行号。这就是说,如果你正在搜索下来,你需要从行号中减去x,这样视口就在包含文本的行之前的一行上定位。


I have a find function that locates a string in a JTable with quite a few thousand entries. Michael Meyers was kind enough to help me out with the goto portion of the function. There appears to be a bug though...

When the user searches for the string the application correctly finds the line in the JTable and highlights it. It also attempts to focus on it, but does not always. Sometimes it will jump 10+ lines short of the line I am looking for and I need to scroll down to see it. As I said, there are a few thousand entries in this JTable and if I'm searching for something, it's difficult to scroll around. Is it possible to focus the selected entry in the center of the visible area?

if (logs.get(i).getLine().contains(findStr))
{
    logTable.scrollRectToVisible(logTable.getCellRect(thisPos, 1, true));   // goto
    logTable.setRowSelectionInterval(thisPos, thisPos);                     // highlight
}

I'm not sure that it helps any, but here is the JTable setup code:

JTable logTable = new JTable(logTableModel);
logTable.setShowGrid(true);
logTable.setShowVerticalLines(true);
logTable.setShowHorizontalLines(false);
logTable.setRowSorter(sorter);
logTable.getSelectionModel().addListSelectionListener(new LogRowListener());

JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().add(logTable);
scrollPane.setPreferredSize(new Dimension(800, 450));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

Thanks

EDIT Below is a link to download a .jar file. This file is a limited version of the code that illustrates the problem. This version seems to consistently jump 2-3 lines short, that is not always the case in the full version.

Demo.jar

The code even for this demo is still a few hundred lines, so below is the sections that I believe are relevant.

public class Proto extends JFrame implements ActionListener
{
    public Proto() { ... }

    @Override
    public void actionPerformed(ActionEvent event) 
    {
        String command = event.getActionCommand();

        if (BUTTON_NEXT_FIND.equals(command)) 
        {
            findNext();
        }
    }

    ...        

    private void findNext()
    {
        String findStr = findField.getText();

        int pos = selectedLogRow;

        // if we're searching for the same string again step forward once
        if (pos == lastFoundPos)
            ++pos;

        // search through the log for the string 
        while (pos < logs.size())
        {
            if (logs.get(pos).getLine().contains(findStr))
            {
                logTable.scrollRectToVisible(logTable.getCellRect(pos, 1, true));
                logTable.setRowSelectionInterval(pos, pos);
                lastFoundPos = pos;
                break;    
            }
            ++pos;
        }
    }

    ...
}

解决方案

Sometimes it will jump 10+ lines short of the line I am looking for and I need to scroll down to see it.

Post your SSCCE that demonstrates the problem. You can just create a table with a thousand rows and then use setValueAt(...) method to populate a few random cells with the text you are searching for.

Is it possible to focus the selected entry in the center of the visible area?

You need to adjust the row number you want to scroll to. That is if you are searching down you would need to subtract "x" from the row number so the viewport is position on a row before your row that contains the text.

这篇关于Java JTable转到行Bug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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