Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > jtable/jlist selection using keyboard

Reply
Thread Tools

jtable/jlist selection using keyboard

 
 
steve
Guest
Posts: n/a
 
      11-03-2005
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

 
Reply With Quote
 
 
 
 
zero
Guest
Posts: n/a
 
      11-04-2005
steve <(E-Mail Removed)> wrote in news:(E-Mail Removed):

> 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/tutor...nts/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/tutor...nents/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.
 
Reply With Quote
 
 
 
 
steve
Guest
Posts: n/a
 
      11-04-2005
On Fri, 4 Nov 2005 09:01:05 +0800, zero wrote
(in article <Xns970414870B0F6zerothishi@195.130.132.70>):

> steve <(E-Mail Removed)> wrote in news:(E-Mail Removed):
>
>> 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/tutor...nts/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/tutor...nents/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();
}

 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      11-05-2005
steve <(E-Mail Removed)> wrote in news:(E-Mail Removed):

>
> 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?
 
Reply With Quote
 
steve
Guest
Posts: n/a
 
      11-05-2005
On Sat, 5 Nov 2005 09:21:28 +0800, zero wrote
(in article <Xns970517FA87AB7zerothishi@195.130.132.70>):

> steve <(E-Mail Removed)> wrote in news:(E-Mail Removed):
>
>>
>> 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?


nope ,obviously i had to code the class, but my biggest problem was the
keypresses.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
selection structure nested within another selection structure 4Ankit@gmail.com Javascript 1 12-07-2006 11:47 AM
JTable with row selection, but no cell selection Simon Niederberger Java 2 01-07-2005 04:17 PM
JS comparing innerHTML to text selection (window.getSelection() /document.selection) Andrew Crowe HTML 1 09-13-2004 02:22 PM
How to change a range selection to text selection? Loebb Javascript 0 02-23-2004 02:12 PM
HOWTO autopost the selection list upon selection curiousity ASP .Net Mobile 0 11-21-2003 12:57 AM



Advertisments