Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent JTable cell editor from leaving "edit mode" on validation failure

Tags:

java

swing

I'd like to obtain the following behaviour in a JTable:

  • user starts editing a cell - the cell switches to a JTextField as usual;
  • when the user tabs out or clicks in another cell I'd like to validate the input and give a visual clue when validation fails (for example change the background color of JTextField) and prevent the cell from commiting AND switching back to default renderer (JLabel).

So far I've overridden DefaultCellEditor. I can hook into getCellEditorValue and make the validation there, but I don't know how to prevent the cell from going back to the default renderer.

Thanks!

like image 799
Bogdan Avatar asked Oct 22 '25 13:10

Bogdan


1 Answers

What about JTable's method editingStopped()? See also

/**
        * Stops editing and
        * returns true to indicate that editing has stopped.
        * This method calls <code>fireEditingStopped</code>.
        *
        * @return  true 
        */         
     public boolean stopCellEditing() { 
        fireEditingStopped(); 
        return true;
    }

in the DefaultCellEditor

like image 105
StanislavL Avatar answered Oct 24 '25 03:10

StanislavL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!