Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Although not the identical question, I was searching through the (too many) questions like this one. In my case I wanted to be able to fill two other fields (jtfStarePatReq and jtfStarePatFound which are JTextFields) either by looking up an index directly or by using some spinners and creating a string and then looking that string up (okay, maybe that is too vague but I think it is enough context). What I wanted is that if the user deleted or cleared out the value in the JFormattedTextField jftfStareOpsIndex, then the other two fields would be cleared as well. I was using the .isEmpty() method on the JFormattedTextField to decide if I should use that field OR use the longer computed search method. So I NEED it to be empty if someone goes from looking up their own index to letting the software search for the index. Anyway, I tried catching the commitEdit() exception and setting the value to null and it seems to do the trick.</p> <pre><code>public class stareOpsIndexListener extends KeyAdapter implements FocusListener { public void focusGained(FocusEvent e) { } public void focusLost(FocusEvent e) { try { JFormattedTextField jftf = (JFormattedTextField) e.getComponent(); jftf.commitEdit(); updateStareOpsIndex(jftf); } catch (ParseException ex) { jtfStarePatReq.setText(""); jtfStarePatFound.setText(""); jftfStareOpsIndex.setValue(null); } } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_ENTER) { try { JFormattedTextField jftf = (JFormattedTextField) e.getComponent(); jftf.commitEdit(); updateStareOpsIndex(jftf); } catch (ParseException ex) { jtfStarePatReq.setText(""); jtfStarePatFound.setText(""); jftfStareOpsIndex.setValue(null); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload