• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable Custom Editing

 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTable and I want to add in two different forms of customization.

First, I want to validate that the String they enter into a date column is a valid date and not allow them to stop editing until they either enter a valid date or hit a key to clear the field and move on without putting anything in it.

Second, on some fields I know the exact length of the field. For example, for a column named "Type of Service" it will always be two digits. So I need to two things, one ties in with the first and it is validate that they entered two digits, the second is as soon as they enter in the second digit I want to move to the next field.

Not sure how to make this work with a custom editor. The validation shouldn't be too difficult, but how do I go about moving to the next field when they hit the second digit?
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try component.requestFocus() to move to the next component after you do your validation.

EDIT

Ok that was stupid! Not enough caffine yet. (Wake up Steve!)

Using the code below you will know where you are at in the table.



So what I'd do is to use setValueAt
to move to the next field. I'd check if there was already a value there and just set it back to what it was just to get the focus in the field.

Anyone else have a better solution?

Steve (slowly waking up)
[ October 11, 2005: Message edited by: Stephen Boston ]
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, better yet, use editCellAt(row, column +1)
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephen Boston:
Or, better yet, use setEditingColumn(column + 1)
[ October 11, 2005: Message edited by: Stephen Boston ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I failed to explain the part of it that I have difficulty with. I know how to determine what cell I'm in, I know how to determine what cell to move to next, what I don't know is where to put the code that does this.

I need to determine when the value is 'complete' which I'm sure I can do with a KeyListener or something else. But what then? Do I call stopCellEditing() and then editCellAt() once it reaches this point? That seems circular to me, since that's effectively telling the table to stop editing from a class that is the editor.

Perhaps something like adding a KeyListener to the JTextField I'm using as the editor and checking the length with each keyTyped()? I'm not sure that would work though. The reference to the JTable is passed along when it gets the editor, and that wouldn't be within the scope of the KeyListener. I could save it to a local variable, but then I'm just operating on the most recent JTable to get the editor, which may or may not (I don't know) be a safe assumption.
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken, I'm at a loss on how to help with that. And by looking back at my posts on this, I think I must have been brain dead that day.


Maybe a keylistener like you said that checks the length of the value in the cell and then when its at the limit you want (lets say two) fires a action to jump to the next cell?
 
Andreas Ecker
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

concerning the problem with the date field, I had a similar thing recently.
What I did was: extend javax.swing.DefaultCellEditor to a custom class, extend javax.swing.DefaultCellEditor.EditorDelegate (inner class) to a custom class, which overrides stopCellEditing() to check, if the value is a valid date. If yes, return true, if no return false to keep the field in editing mode.
You should check the Java source files, where/what you actually have to change.
This was not a very beautiful solution, but it did work.

Andreas
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic