jtable/jlist selection using keyboard

Discussion in 'Java' started by steve, Nov 3, 2005.

  1. steve

    steve Guest

    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
     
    steve, Nov 3, 2005
    #1
    1. Advertising

  2. steve

    zero Guest

    steve <> wrote in news::

    > 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.
     
    zero, Nov 4, 2005
    #2
    1. Advertising

  3. steve

    steve Guest

    On Fri, 4 Nov 2005 09:01:05 +0800, zero wrote
    (in article <Xns970414870B0F6zerothishi@195.130.132.70>):

    > steve <> wrote in news::
    >
    >> 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.




    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();
    }
     
    steve, Nov 4, 2005
    #3
  4. steve

    zero Guest

    steve <> wrote in news::

    >
    > 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?
     
    zero, Nov 5, 2005
    #4
  5. steve

    steve Guest

    On Sat, 5 Nov 2005 09:21:28 +0800, zero wrote
    (in article <Xns970517FA87AB7zerothishi@195.130.132.70>):

    > steve <> wrote in news::
    >
    >>
    >> 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.
     
    steve, Nov 5, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Niederberger
    Replies:
    2
    Views:
    16,535
    Christian Kaufhold
    Jan 7, 2005
  2. John_Woo
    Replies:
    3
    Views:
    1,472
    Andrew Thompson
    Oct 3, 2006
  3. evgchech
    Replies:
    0
    Views:
    797
    evgchech
    Feb 26, 2007
  4. oziris
    Replies:
    0
    Views:
    351
    oziris
    May 9, 2007
  5. loris_p
    Replies:
    2
    Views:
    1,256
Loading...

Share This Page