File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable Custom Editing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Custom Editing" Watch "JTable Custom Editing" New topic
Author

JTable Custom Editing

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jul 14, 2005
Posts: 165
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 ]

Steve<br /> <br />No matter where you go, there you are.<br /> <br />"My evil self is at the door, and I have no power to stop it."
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
Or, better yet, use editCellAt(row, column +1)
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
Originally posted by Stephen Boston:
Or, better yet, use setEditingColumn(column + 1)
[ October 11, 2005: Message edited by: Stephen Boston ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jul 14, 2005
Posts: 165
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

Joined: Jun 13, 2002
Posts: 4
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable Custom Editing