问题
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;
}
}
...
}
回答1:
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.
来源:https://stackoverflow.com/questions/4632742/java-jtable-goto-row-bug