Results 1 to 4 of 4
  1. #1
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default Urgent:Jtable and key Listener

    Hi,

    I have a jtable with few rows. I have implemented a key listener on it. Whenever I press Enter key from keyboard, it should open up a dialog box relevant to that row. But when I select a row and press enter, the row selection moves to next row. It works fine when multiple rows are selected.

    public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    if(keyCode == KeyEvent.VK_ENTER){
    //perform the action
    }
    }

    Please suggest me something so that row selection remains the same, it does not move to the next row.

    Thanks in Advance,
    Simmi

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by simmi View Post
    Hi,

    I have a jtable with few rows. I have implemented a key listener on it. Whenever I press Enter key from keyboard, it should open up a dialog box relevant to that row. But when I select a row and press enter, the row selection moves to next row. It works fine when multiple rows are selected.

    public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    if(keyCode == KeyEvent.VK_ENTER){
    //perform the action
    }
    }

    Please suggest me something so that row selection remains the same, it does not move to the next row.

    Thanks in Advance,
    Simmi
    The JTable already has an action bound to its enter key press, and to override this, I think that you'll have to bind a different action. Thus I believe that the solution to your problem is to use keybinding, not a key listener. You may have to experiment to figure out which input map to use (I think that you may need the JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT). For more details on how to use key binding, please check out the tutorial: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    The Enter key is indeed a key that JTableUI recognizes for moving to the next row.
    Have you tried consuming the event?

    Java Code:
    public void keyPressed(KeyEvent e) {
       int keyCode = e.getKeyCode();
          if(keyCode == KeyEvent.VK_ENTER){
          //perform the action
          e.consume();
       }
    }

  4. #4
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default

    Hey Fubarable,

    Thanks alot. It worked for me. :)

    I did the following stuff in my code.

    myTable.getInputMap().put(KeyStroke.getKeyStroke(
    KeyEvent.VK_ENTER, 0),
    "enter");
    this.getActionMap().put("enter", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
    //action to be performed
    }
    });
    }

    Thanks once again.

    Simmi

Similar Threads

  1. Tab Listener
    By teckno101 in forum AWT / Swing
    Replies: 2
    Last Post: 09-29-2009, 09:40 PM
  2. JTable Cell Focus Listener
    By Manfizy in forum New To Java
    Replies: 1
    Last Post: 07-21-2009, 08:08 AM
  3. add an undo listener to a Jtable
    By christina in forum Advanced Java
    Replies: 12
    Last Post: 01-29-2009, 07:47 AM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-20-2008, 11:07 PM
  5. Replies: 2
    Last Post: 05-15-2008, 10:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •