Results 1 to 3 of 3
  1. #1
    javaexplorer is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default Start editing in a cell in JTable on gaining focus

    Hi,

    I have defined cell editors for the two columns in my table in the following manner:

    Java Code:
    JComboBox combo = new JComboBox();
    //code to add items to the combo box goes here.
    
    JTextField textField = new JTextField();
    textField.setHorizontalAlignment(JTextField.RIGHT);
    
    TableColumn column = myJTable.getColumnModel().getColumn(0);
    column.setCellEditor(new DefaultCellEditor(combo));
    
    column = myJTable.getColumnModel().getColumn(1);
    column.setCellEditor(new DefaultCellEditor(textField));
    The problem I am facing is that when a focus is moved to a table cell, the cell doesn't become automatically editable. So, when the focus is moved to column 2 (that has a text field as an editor), the caret sign doesn't not appear unless the cell is double-clicked or the user starts typing. Similar is the case for column 1 (that has a combo box as an editor) as here the combo box doesn't appear unless the cell is clicked. These behaviors are counter-intuitive and undesirable for a user operating with the keyboard.:(

    Please suggest pointers on how this could be resolved.

    Thanks in advance.

    -Dinesh

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

    Default

    Override the changeSelection(...) method of JTable. Something like:

    Java Code:
    public void changeSelection(	int row, int column, boolean toggle, boolean extend)
    {
    	super.changeSelection(row, column, toggle, extend);
    
    	if (editCellAt(row, column))
    	{
    		Component editor = getEditorComponent();
    		editor.requestFocusInWindow();
    	}
    }

  3. #3
    javaexplorer is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Smile solved : start editing in a cell in JTable on gaining focus

    Hi Camickr,

    Your code worked perfectly, and the issue is resolved!!! :)

    Many Thanks for that.

    -Dinesh

Similar Threads

  1. JTable Cell Focus Listener
    By Manfizy in forum New To Java
    Replies: 1
    Last Post: 07-21-2009, 08:08 AM
  2. editing jTable then writing new values to database!
    By sweet angle in forum AWT / Swing
    Replies: 6
    Last Post: 01-27-2009, 05:07 PM
  3. Replies: 0
    Last Post: 11-14-2008, 04:14 AM
  4. JTable editing with Mouse
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 0
    Last Post: 10-11-2008, 08:34 AM
  5. jTable Cell editing
    By mahaling_m in forum AWT / Swing
    Replies: 0
    Last Post: 06-23-2008, 08:17 AM

Tags for this Thread

Posting Permissions

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