• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Validating JTable Cell

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using this as a TableCellEditor, you can catch invalid values when they finish editing in the stopCellEditing() method.
 
Joshua Doerring
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joshua Doerring
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great the stopCellEditing
 
Joshua Doerring
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic