1

We currently have a focus problem with a JTable/JTextEditor in java swing. The JTable has a custom cell editor which is a JTextField.

The issue is when a cell is being edited and contains invalid data, and the user clicks on a JButton, the text field will stop editing and the JButton actionPerformed (clicked) is called. The JTable#setValueAt handles validation so if the data in the JTextField is invalid, the underlying TableModel is not updated.

Ideally, we do not want to let the JButton click occur. Focus should remain with the JTable or the JTextField.

Clicking the button will perform a submit action and close the frame the table is in. As the validation in the TableModel#setValueAt does not update the value, it submits the old value.

Can this be done? I am still fairly new to Swing so I am not aware what to check.

Unfortunately, our code is not straight forward. The UI is constructed from XML in such a way that the button knows nothing about anything else on a form (this is code I have inherited).

In .net you could stop a control losing focus by handling a Validating event and setting a cancel flag. Is there a similar mechanism with Java.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Andez
  • 5,588
  • 20
  • 75
  • 116
  • for better help sooner edit your question with an [SSCCE](http://sscce.org/) – mKorbel May 23 '12 at 11:18
  • Could you perhaps tell us the purpose of the button? Why would users click it and why is it there in the first place? Can't you simply disable the button while they are typing? Help us understand the specific use case, and we can help you a bit more accurately. – Ewald May 23 '12 at 11:37
  • Cheers guys. Edited question slightly to help try and paint a picture. I am more used to .NET and due to the complexity of our system, I cannot really post a code snippet. I will however try and write a simple scenario up with a table and a button. – Andez May 23 '12 at 11:53

4 Answers4

4

Validating the input after editing has concluded, in setValueAt(), may be inconveniently late. The editor itself can preclude navigation for invalid values, as shown in this example that links to the corresponding tutorial section.

For valid values, you can make the table commit when losing focus:

table.putClientProperty("terminateEditOnFocusLost", true);
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Hi trashgod, client properties seem interesting, but they appear to be undocumented in Java. And are component specific. Do you know any good resources that list these properties per component? Thanks – Andez May 24 '12 at 11:08
  • Check the `JTable` source, near `CellEditorRemover`. – trashgod May 24 '12 at 12:53
0

Can you try using inputverifier on the editor component, i.e. text field?

DKumar
  • 352
  • 1
  • 3
  • 13
0

When the focus is lost from a component, the lost focus method is called (more reference in http://docs.oracle.com/javase/tutorial/uiswing/events/focuslistener.html). Therefore, you may call the validation method when you lose the focus.

If you do not need to be aware of the specific field being edited, you can also perform validation inside your button and prevent the submission if it is not sucessful.

rlinden
  • 2,053
  • 1
  • 12
  • 13
0

I'd achieved a similar functionality by overriding the stopCellEditing method in my JTable's CellEditor.

@Override
public boolean stopCellEditing() {
   String s = (String) getCellEditorValue();       
   if (s != null) {           
       if (!testYourValue()) {           
           Toolkit.getDefaultToolkit().beep();           
           return false;
       }
   }
   return super.stopCellEditing();
} 
Peter
  • 5,728
  • 20
  • 23
  • Overriding `stopCellEditing` won't work. When the table loses focus it doesn't matter what is in your `stopCellEditing` method the cell-editor will close and the cell's value will revert back to what it was. – wcmatthysen May 21 '19 at 22:03