Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Validating JTable Cell Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Validating JTable Cell" Watch "Validating JTable Cell" New topic
Author

Validating JTable Cell

Joshua Doerring
Greenhorn

Joined: Aug 08, 2003
Posts: 19
For a particular project I'm working on the specs say that as a user types a value into a particular cell in a table, I must validate that each character is valid and that they are typing a valid code.
Example: Say valid codes are "01", "02", "10", 11", "20", ... we actually have a large number of codes and not all are numeric.
I have the part about validating as they type, if they type a 0 ... no problem so far so they type as the next character a 3 ... well there's no 03 code in the list so I make the "ding" sound and I don't even allow the 3 to show in the field. they would still see the 0 and the cursor waiting for the next char. and if they then typed 1 ... thats ok because 01 is a valid code. And in this example there are no codes larger than 2 digits so if they tryied to type any more i just make the "ding" and don't add the character.
The checking as they type works great they cannot enter a bad code... what I didn't think about is what if they just type a 1 and then exit the field. It wasn't an error because the char 1 was a valid begining to the code "11" but wasn't the complete code.
What I need to do is a second check when they're done editing the field to see if the code currently there is valid. and if it is not, do not leave the edit portion of the field and stay positioned to the last character and make the "ding" sound just as if they had typed something invalid.
For anyone's curriosity here is the TextField class that I create that columns cell editor from

Please help!
Thanks
Robert Kellogg
Greenhorn

Joined: Feb 11, 2004
Posts: 2
Hi Joshua,
There seem to be a feww simple alternative answers to your question. Why not make the "editor" a JComboBox. The box would only have the "valid" choices to choose from. Or, how about a Spinner. (I have used a Combo box editor, but not a spinner. However, it seems quite doable.) Baring this, how about a formatted text field?
I hope that this helps.
BobKel66
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

If you are using this as a TableCellEditor, you can catch invalid values when they finish editing in the stopCellEditing() method.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Joshua Doerring
Greenhorn

Joined: Aug 08, 2003
Posts: 19
I would like to use a JComboBox but the powers that be say they want it to be entered manually. Also, the example above would be simple enough but some of the values for other areas range in the thousands. and typing would typicall be much faster than the drop-down-and-scroll-til-you-find-it method.
And, I am not familiar with what you mean by Spinner.

For the stopCellEditing() method I have a test program I tried that with a couple of days ago and stragely enough that event was fired upon double clicking in the field and not after exiting the field and it was only fired once. double clicking and entering a new value did not retrigger the event.

In this example I was specifically checking for A01 as being the only valid value and if any thing else was entered it would "ding" and would remaing being edited.
Thanks so far for the suggestions.
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Hi Robert,
I am also very interested in how you go with this. I have not been able to reliably catch the end of the edit process. Further, I also wanted to start the edit with a single click or just when the user entered the field with the tab key. I tried using a renderer extending a JTextField (I was desperate!) but finished up drawing in my own JTextFields in a table arrangement and removing all references to JTable. I you find a solution I could revert to JTable. This table shares a JPanel with a heap of JPanel/JTextField pairs using focus listeners do do some validation (so easy!), so now the simulated table fits in better with the rest of the page.


The nice thing about Standards is that there are so many to choose from!
Joshua Doerring
Greenhorn

Joined: Aug 08, 2003
Posts: 19
I did a little looking and I now know what a Spinner is. The only problem is that our company is currently restricted to using the IBM JDK 1.3.1 which is pretty much identical to Java JDK 1.3.1 but Spinner comes in 1.4 and it still wouldn't solve the problem with the fields that could contain thousands of values.
Thanks for the Spinner idea though, as soon as we start using 1.4 I'll have a few ideas of where that can be used in our current product.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

That shouldn't happen with stopCellEditing()... instead of using super.stopCellEditing() in your code, try replacing it specifically with 'true'.

Also, try this example -
Joshua Doerring
Greenhorn

Joined: Aug 08, 2003
Posts: 19
Great the stopCellEditing
Joshua Doerring
Greenhorn

Joined: Aug 08, 2003
Posts: 19
oops. wrong button. What I was going to say is that the stopCellEditing() method works great for what I'm trying to accomplish for some reason when you setCellEditor for the table instead of the individual column it doesn't work and thats what I was doing in my example.
The only thing left is when they select another row it still changes the selection to that row and the blinking cursor no longer is in the editing field but it is still editable. so I have a little more work to do on this but it is getting very close.
Thanks for the help.
Joshua Doerring
Greenhorn

Joined: Aug 08, 2003
Posts: 19
A little more information I am using the above code but I changed this

instead of

other wise when you do enter a good value the box still looks like its in edit mode.
back to the point about the selected row, this only seems to be a problem with the mouse because when you hit tab or enter or left/right up/down it dings and acts like I want it to. so apparently it has to do with the mouse and how tables handle the mouse making selections
any ideas.
Joshua Doerring
Greenhorn

Joined: Aug 08, 2003
Posts: 19
some one suggested this to me... override the changeSelection method of JTable

This works for the most part. Only one problem with this is inconsistant behavior. If there is an invalid value and you press down it "dings" and the caret stays where it is like it should but if you click another cell with the mouse, you get the "ding" but the caret is nolonger visible. On the same note if I start typing it append the new characters but still no caret. Kinda does the same thing if you hit the tab key, only difference here is, say I have 2 columns per row, if I hit tab once it "dings" then the caret dissappears, but if I tab a second time there is no "ding" but the caret reappears. like it tabbed to the second column then returned.
This is very strange.
Thanks for everyone's help so far.
Joshua Doerring
Greenhorn

Joined: Aug 08, 2003
Posts: 19
modifying the above code just a little almost get me the results I need.

I tried to have the DefaultCellEditor request focus but it never occured to me to have the component it contains request focus. Also, there is still a little problem with how the TAB key reacts to this though. If I enter an incomplete value then press TAB, I still loose the focus and it does not "ding" and if I press TAB any number of times after that it does "ding". But the field is still editable and I can still enter more values.
Everything else works perfectly how I need it. Pressing up/down, left/right, clicking another row with the mouse. It all works great.
A little more FYI pressing the TAB key in this case does not call the changeSelection() method.
Thanks for all the help so far everyone. I'm going to see if I can sneek this through testing with out anyone noticing the TAB thing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validating JTable Cell
 
Similar Threads
FocusListener
Help with JFormattedTextField!!!
Questions on Fixed-Size JTextField
JOptionPane/JDialog question
NX:Formatted textField