Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

search function in JTable  RSS feed

Robert Ingmarsson
Posts: 27
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best ranchers!

I am trying to write a search function for a JTable in one of my projects. Im struggling really hard with this. Can anyone help me to see what i am missing?
Or maybe point me in the right direction. I want to loop through all columns in my JTable and match the data in theese with values from a JTextField.

What i have tried so far -  however this attempt was unsuccessful - is the following lines of code. The goal is to highlight the row with the columns in my JTable
that contains the value of the JTextField everytime after the user has clicked a search button. My approach to this problem maybe was totally wrong. I don't
wan't to use the TablrRowSorter or RowFilter classes as a search function just a simple highlight of the row that contains the value that was entered into
the JTextField.

The code below doesn't find the row desired row it only highlight some row two or more steps above it randomly. Maybe i should look into cell renderers
a little deeper but than again i need help with this one. please.

Maneesh Godbole
Posts: 11445
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API Javadoc
public void setRowSelectionInterval(int index0,
                                    int index1)
Selects the rows from index0 to index1, inclusive.

This sets the current selection range

However, there is another method
public void addRowSelectionInterval(int index0,
                                    int index1)
Adds the rows from index0 to index1, inclusive, to the current selection.

This adds to the existing selection range.

Try using the second method
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!