Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3660784
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:14:23+00:00 2026-05-19T01:14:23+00:00

I have a find function that locates a string in a JTable with quite

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:14:23+00:00Added an answer on May 19, 2026 at 1:14 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.