jtable/jlist selection using keyboard

S

steve

Hi,

on most other os file selectors/table selectors , pressing the first letter
of the selection takes you to that selection range.

IE if you press "P" you are taken to any item where the first letter is "P".

Is there any code floating about to implement this in a JTable?

steve
 
Z

zero

steve said:
Hi,

on most other os file selectors/table selectors , pressing the first
letter of the selection takes you to that selection range.

IE if you press "P" you are taken to any item where the first letter
is "P".

Is there any code floating about to implement this in a JTable?

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.
 
S

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.



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();
}
 
Z

zero

steve said:
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();
}

you found a class that does it? I guess the firesearch method scrolls down
to the correct index in the sorted JTable right?
 
S

steve

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top