*
The moose likes Swing / AWT / SWT and the fly likes Change of background colours on validation from SetValueAt in Jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Change of background colours on validation from SetValueAt in Jtable" Watch "Change of background colours on validation from SetValueAt in Jtable" New topic
Author

Change of background colours on validation from SetValueAt in Jtable

Jo Pras
Greenhorn

Joined: Feb 11, 2013
Posts: 2
I have a UI class which has creates a jtable. The Table Model is placed in a different class. All the values in the table is editable. On editing, I need to validate the value. If the validation fails then the cell is to be changed to RED color and if found correct the cell should be in white background . Can anyone help me with this??
Loic Beylot
Greenhorn

Joined: Mar 24, 2011
Posts: 8
Jo Pras wrote:I have a UI class which has creates a jtable. The Table Model is placed in a different class. All the values in the table is editable. On editing, I need to validate the value. If the validation fails then the cell is to be changed to RED color and if found correct the cell should be in white background . Can anyone help me with this??


To do this you can use a renderer (creating a class that extends 'DefaultTableCellRendere'), which modify the colour of the cell if its value is not correct.

Have a look at this webpage.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
This is done by the editor. The default editors do this by using a RED Border to highlight the cell in error. Try entering an invalid integer value in this table:



Here is an example of how you can do this when you create a custom editor. In this table you get an error unless you enter a 5 character string:

Jo Pras
Greenhorn

Joined: Feb 11, 2013
Posts: 2
Thnks Rob !!!
There is some dynamic validation to be called when the value is entered. Can you suggest where it need to be called?

For e.g :- If the value is double then i need to check the range and validate if the value can be set ?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
I already gave you an example. My check was to make sure the string entered was 5 characters long.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Change of background colours on validation from SetValueAt in Jtable
 
Similar Threads
Color in JTable
JTABLE
jtable
JTable
JTable Trouble