Results 1 to 10 of 10
  1. #1
    khadaree is offline Member
    Join Date
    Mar 2009
    Posts
    10
    Rep Power
    0

    Default JTable naviagation problem

    Dear friends,
    I have created a jtable and i want to change the enter key functionality to tab key. when i press enter the focus has to move to next column cell. the main thing is i also have custom cell editor and cell renderer of jtextfield.

    if i use actionlistener or keylistener in celleditor in getTableCellEditorComponent() method, when i press enter it is firing as many rows available in the respective column. i spent two days on these. please some one help me out.

    code looks like this.
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    table = new JTable(4,4) ;
    table.setCellSelectionEnabled(true);
    TestRenderer trend = new TestRenderer();
    TestEditor tedit = new TestEditor(this);
    tcol = table.getColumnModel().getColumn(0);
    tcol.setCellRenderer(trend);
    tcol.setCellEditor(tedit);
    tcol = table.getColumnModel().getColumn(1);
    tcol.setCellRenderer(trend);
    tcol.setCellEditor(tedit);
    tcol = table.getColumnModel().getColumn(2);
    tcol.setCellRenderer(trend);
    tcol.setCellEditor(tedit);
    tcol = table.getColumnModel().getColumn(3);
    tcol.setCellRenderer(trend);
    tcol.setCellEditor(tedit);
    table.setSurrendersFocusOnKeystroke(true);
    JScrollPane js = new JScrollPane();
    js.setViewportView(table);
    getContentPane().add(js);
    setSize(400,500);
    setVisible(true);


    cell editor code is,

    public Component getTableCellEditorComponent(JTable table,Object value,
    boolean isSelected,int row,int column) {
    if (value == null) {
    setText("");
    }
    else{
    setText(value.toString());
    }
    return this;
    }

    Thanks,
    Khadaree
    Last edited by khadaree; 07-14-2009 at 04:13 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    See Keyboard Operation for Tables for the default keyboard operations.

    Then How to Use Key Bindings for how to change things according to your requirements.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Unable to post code to this thread.

  4. #4
    khadaree is offline Member
    Join Date
    Mar 2009
    Posts
    10
    Rep Power
    0

    Default

    Hi hardwired,
    I think you are not understood my problem. I want the enter key work like a tab key in Jtable. I have tried adding the actionlistener to the JTextfield in CustomCellEditor and I have tried using Action classes,InputMap() and ActionMaps also. but when i press enter these methods are firing more than one time and the result is random focus in the jtable.
    Please some one help me out.

    Thanks,
    Khadaree

  5. #5
    khadaree is offline Member
    Join Date
    Mar 2009
    Posts
    10
    Rep Power
    0

    Default

    solved the problem by using the following code,

    table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCU SED_COMPONENT).
    put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
    "selectNextColumnCell");


    Thanks,
    Khadree.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I want the enter key work like a tab key in Jtable
    Instead of hardcoding the "selectNextColumnCell" which means you first have to know what string to use, the easier way is to just change the InputMap using the KeyStroke of the Tab key.

    An example of this approach is found here:

    "tips4java-wordpress-com"

    Then search for "Key Bindings"

    (I can't post real links yet)

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Here's the code example I was unable to post the other day. Fubarable suggested attatching it in a zip file.
    Attached Files Attached Files

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

    Default

    Hardwired: What happens when you try to post code here? Perhaps the forum software has a bug that needs to be addressed?

  9. #9
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    What happens when you try to post code here?
    I've narrowed it down to this line:
    ActionM a p actionM a p = target.getActionMap();
    If I remove the spaces in the "M a p" parts of the string I get a 404 page_not_found error when I press the "Preview Post" button.

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

    Default

    I can reproduce the problem; thanks for bringing it to our attention. I will post a link to this in the moderator forum for the forum director to review and hope that he can find a fix.

Similar Threads

  1. JTable problem
    By sandeepsai39 in forum New To Java
    Replies: 2
    Last Post: 06-27-2009, 07:15 AM
  2. Problem With AI and JTable
    By elad_bj in forum New To Java
    Replies: 8
    Last Post: 04-20-2009, 03:24 PM
  3. JTable problem
    By robbertds1 in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2008, 07:34 PM
  4. Replies: 2
    Last Post: 05-15-2008, 11:15 AM
  5. Problem with JTable
    By Felissa in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 10:25 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
  •