Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField In JTable Value Update Problem  RSS feed

 
Andy Holm
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a JTextField within a JTable. The JTextField is set up so that it is of length 1 and always has its current value overwritten. The problem I am seeing is that the value of the text field isn't updated until after it loses its focus. I want it to be updated immediately whenever a user types in a key. Here's the code for the JTextField:

 
Andy Holm
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be noted that the JTextField is added to the JTable like this:



 
Andy Holm
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more interesting thing I noticed is that these two values are not the same. Value 1 is the new value and value 2 is the old value. I want value 2 to be the same as value 1 (ie the new value).

System.out.println("Value1 is: " + codeType.getText());
System.out.println("Value2 is: " + jTableResult.getValueAt(0, 2));
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The table cell editor only writes its contents to the underlying JTable and TableModel after it is "committed". This is usually done by losing focus, but you can do it programmatically by calling stopCellEditing() on the editor. If you want to rollback your changes there is cancelCellEditing().
 
Andy Holm
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked great! Thanks. On a somewhat related note, is there anyway to force a JTable to lose focus programatically other than doing a requestFocusInWindow on another component?
 
Andy Holm
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked great! Thanks. On a somewhat related note, is there anyway to force a JTable to lose focus programatically other than doing a requestFocusInWindow on another component?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!