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

    Default Programmatically starting cell editing in JTable

    Hi,

    I am using the following snippet of code to programmatically start editing a cell in JTable:

    Java Code:
    if (table.editCellAt(row, column))  {
                    System.out.println("success in starting edit");             
                   if ( table.getEditorComponent().requestFocusInWindow()) {
                       System.out.println("focus request succeeded");
                   } else {
                         System.out.println("focus request failed");
                   }
      } else {
                      System.out.println("couldn't starting editing the cell");
      }
    On running the above code, I get the following output:

    success in starting edit
    focus request succeeded

    However, notwithstanding the above output, the editing doesn't start in the specified cell. The cell just don't become the focussed and active cell. Before requesting the focus, I have also tried to clear the current focus by using the following line of code, but without any success:
    Java Code:
    KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
    Kindly suggest how the desired outcome can be achieved.

    Regards,
    Dinesh

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

    Default

    I use code like the following to automatically start editing every time a cell gets focus.

    Java Code:
    JTable table = new JTable(data, columnNames)
    {
    	//  Place cell in edit mode when it 'gains focus'
    
    	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();
    //			((JTextComponent)editor).selectAll();
    		}
    	}
    };
    So maybe the secret is to invoke changeSelection(...) as well before you attempt to invoke the editor.

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

    Default

    Hi Camickr,

    Thanks for your reply.

    I tried invoking changeSelection before invoking the editor, but it didn't yield the desired output. I must add that I am not I have not subclassed JTable, and am invoking changeSelection on JTable from another class.

    Could you suggest some other trick?

    Thanks again,
    Dinesh

Similar Threads

  1. JTable Cell Merg
    By anilkumar_vist in forum AWT / Swing
    Replies: 8
    Last Post: 04-25-2011, 10:32 AM
  2. Start editing in a cell in JTable on gaining focus
    By javaexplorer in forum AWT / Swing
    Replies: 2
    Last Post: 05-05-2010, 08:34 AM
  3. editing jTable then writing new values to database!
    By sweet angle in forum AWT / Swing
    Replies: 6
    Last Post: 01-27-2009, 06:07 PM
  4. JTable editing with Mouse
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 0
    Last Post: 10-11-2008, 09:34 AM
  5. jTable Cell editing
    By mahaling_m in forum AWT / Swing
    Replies: 0
    Last Post: 06-23-2008, 09:17 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
  •