> Hi,
>
[quoted text clipped - 7 lines]
>
> steve
Maybe, but I doubt it would be easy to find, if it does exist.
You would first have to have a sorted JTable of course. Sun has an
example on how to sort tables (I don't recommend trying to implement it
yourself, it's not easy), which it has generously provided for you to use
anyway you want. One way to use it would be to adjust it to suit your
needs, which shouldn't be too hard - not trivial either though. If
you're a beginner this will be a challenging but educational project.
Here's what you do:
1. If you already know how JTables work under the hood, you can skip
this.
check the tutorial at
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Make sure you have at least a basic understanding of TableModels and how
they relate to the JTable (which is just a view of the model).
2. Download the TableSorter from
http://java.sun.com/docs/books/tutorial/uiswing/components/example-
1dot4/TableSorter.java
3. Modify the TableSorter to suit your needs. Examples could be:
permanently sort on one or more columns, prevent sorting on specific
columns, customize the way some columns are sorted, etc.
4. Implement a KeyListener on the JTable. When a key is pressed, check
if it is a character that begins at least one of the cells in the sorted
column. You might be able to speed this up by keeping a list of the
first characters in said column - depending on how your data and sorting
changes.
One final hint that may prevent some frustration on your part: don't
forget that the index of a cell in the model is not the same as the index
in the view - ie the actual table you see is not in the same order as it
is in memory.
steve - 04 Nov 2005 23:59 GMT
>> Hi,
>>
[quoted text clipped - 43 lines]
> in the view - ie the actual table you see is not in the same order as it
> is in memory.
it was easier than i thought it would be.
basically it comes down to:
public void keyTyped(KeyEvent e) {
//concat the search string until it is re-set by the timer
if (System.currentTimeMillis() - saved_ms < TIMETOWAIT) {
searchField = searchField + e.getKeyChar();
}
else {
searchField = "" + e.getKeyChar();
}
firesearch(null);
saved_ms = System.currentTimeMillis();
}
zero - 05 Nov 2005 02:21 GMT
> it was easier than i thought it would be.
>
[quoted text clipped - 11 lines]
> saved_ms = System.currentTimeMillis();
> }
you found a class that does it? I guess the firesearch method scrolls down
to the correct index in the sorted JTable right?
steve - 05 Nov 2005 11:11 GMT
>> it was easier than i thought it would be.
>>
[quoted text clipped - 14 lines]
> you found a class that does it? I guess the firesearch method scrolls down
> to the correct index in the sorted JTable right?
nope ,obviously i had to code the class, but my biggest problem was the
keypresses.