I want to write a very simple JTable.
The user can edit the cells by entering a valid double. If the user's input is invalid (i.e. they enter a string), I want my table to throw out the invalid input, show the last good input, and display an error message to the user.
I also want the numbers in the table to be formatted like so: "###.##%"
How can I go about something doing this?
Additional Info
I know bits and pieces of what I need to do, but am having trouble connecting the dots.
For instance, I know that to get the formatting correct, I have to subclass DefaultTableCellRenderer
however I am not really sure of exactly what I need in the getTableCellRendererComponent()
method.
I am pretty sure I will have to subclass DefaultCellEditor
to get the editing and error checking to work properly. I have read a lot of different stuff about this though. I am not sure where the best place would be to check for valid input. I have also read about InputVerifier
and am not sure how to use this or if I even should.
A small example of how something like this would work and a brief explanation of the different classes/methods that I have to extend/override would really help me.
Thanks
See Table Format Rendering for the easy way to do this. This approach simply overrides the
setValue(...)
method of the renderer.Here is some old code I found on a forum somewhere that creates a custom IntegerEditor. The first column uses the default Integer editor and the second column uses the custom editor.
If you just want to use the default Double editor, then you just need to change the column class to be Double.class for your column. If you need special editing then you will need to customize the IntegerEditor for your double requirements.