Searching in a JTable
Question 1:
I have a JTable that gets its data from a MySQL database. I read in the data and put it in a 2-dimensional Object array through a loop. The JTable is then constructed with the 2d array and a String array as arguments, and displayed in a JFrame.
Example of a 3x3 table, as displayed:
| Lisa | Olsson | 24 |
| Lisa | Smith | 28 |
| Norman | Jackson | 25 |
In the same JFrame, I have a search field and a search button. What I want to achieve is that when a user for example types "Lisa Olsson", it automatically selects the row that contains Lisa Olsson. I also want to be able to type "Olsson Lisa", or even "LisaOlsson" (though the latter isn't all that necessary).
The selection of the row is something I can handle, but the actual search seems really hard. Can anyone please tip me in the right direction of how to achieve this?
Question 2:
How can I break up a string like this "Lisa Olsson" into two separate strings, "Lisa" and "Olsson"?
Thank you in advance!
# 1
1. String.valueOf(getValueAt(...)).equals(...);
2. string.split(" ");
You can do more sophisticated operations if you want such as 'contains' or word-boundary matching with regexes - you can also use them to perform more sophisticated parsing (eg treating "Lisa Olssen" - where the string includes the quote marks - as an unbroken string).
# 2
Thanks alot for your reply. What I did on the first problem was to take the object 2d array that was created after I've read it in from the database, and convert the whole thing to a string 2d array. Through two loops I first compared each cell in a row to the search word using the string contains() function, then reset the cell counter to 0, incremented the row counter with +1 and did the while thing over again. Worked perfectly for what I want to do as long as I have 1 keyword.
When I get back to work on monday I'll try your split function. When I get it all to work, I think I'll cook up a little tutorial and post it on my webby. Maybe make a JTableSearch class that takes misc. inputs.
Again, thank you :)
# 3
Thanks alot, Im trying to incorporate your ideas. What I*ve made now is a search class that takes a search word and a 2 dimensional String array as input, and returns the row that the current word is at.
This works great for single word searches, but what I think I'll have to make is several versions of this function. The next function Im going to make will return an int array instead. If the search word is found in several rows (right now it stops at the first find), it returns all of the row numbers/indexes in the int array. If there are two search words, I will have a function that executes that also takes the int array as an input, and only searches through the rows at the given indexes after the second word. If there are more than 2 words, rince and repeat.
This is my search class at the moment:
public class EMSearch
{
// GLOBAL
int ROWDIM = 50;
public int contains(String search, String[][] searchtable)
{
boolean found = false;
boolean searchrow = true;
int cellcounter = 0;
int cellcount = 4;
int row = 0;
int cell = 0;
int[] cellhit = new int[ROWDIM];
while(!found)
{
while(searchrow)
{
if(cellcount == cell)
{
cell = 0;
searchrow = false;
}
else
{
System.out.println("Array value:"+searchtable[row][cell]);
if(searchtable[row][cell]!=null)
{
if((searchtable[row][cell]).contains(search))
{
System.out.println("Found "+searchtable[row][cell]);
found = true;
return(row);
}
}
cell++;
}
}
row++;
searchrow = true;
if(row==50)
{
System.out.println("Search not found.");
found = true;// return true value to break the loop;
}
}
return(99999);
}
}