• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable-nice editing for a Double value

 
Paul Bailey
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, has anyone come up with a nice way to edit a double value in a JTable? Presently, it is just a text box and tabing into the column makes editing a pain--if you tab into a row with 0.0 and you type 3 you get 0.03 instead of the expected 3 and when you tab into 10 and type 3 you also get 10.03 instead of the expected 103 or even the somewhat more sensical 10.3.
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Try putting a KeyListener in the table and then on any KeyPressedEvent where the key is tab , clear the text in the arriving cell ( storing it in a temp String ) and if the user keys something in the value will be correct.
If the user types tab again, place the text in the cell again and clear the next cell,.....etc.
this snippet has the structure for the code you need
// placed in the KeyListener KeyPressed method.
if( e.getKeyCode() == KeyEvent.VK_TAB )
//place tempString back in previous cell.
//place current cell text in tempString.
// clear curent cell.
Then you'll need to handle the input if the user types anything so that any input is reflected in the tempString so that when the user moves on, the old text isn't placed in that cell.
Hope that helps,
Terry
 
Paul Bailey
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terence,
The JTable already has a KeyListener that handles most of this stuff and i think this method would work but you would have to figure out which listener gets mesages first and make sure that you aren't stepping on its toes, et cetera.
I found that trying to caste Doubles as Integers works well. This code is not optamized

This code is not elegant. But it works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!