Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using your <a href="http://sscce.org/" rel="nofollow">sscce</a>, I am unable to reproduce the effect you describe on Mac OS X, Java 6, which supports @CatalinaIsland's observation. In particular, focus never leaves an empty text field using either <kbd>tab</kbd> or <kbd>shift-tab</kbd>; focus becomes <code>null</code> only when the frame is deactivated.</p> <p>I see two threads accessing multiple fields with no synchronization at all. At a minimum, you should use <code>EventQueue.invokeLater()</code> in <code>t</code> to update the GUI, as described in <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html" rel="nofollow"><em>Concurrency in Swing</em></a> and show below.</p> <p>The broader question is this: What focus problem are you trying to solve using <code>t</code>?</p> <pre><code>import java.awt.Component; import java.awt.DefaultKeyboardFocusManager; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.KeyboardFocusManager; import javax.swing.InputVerifier; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class FocusDemo { private static final JTextField txtOne = new JTextField("Ignore this control"); private static final JTextField txtTwo = new JTextField("Delete this text, then press shift-tab"); private static final JLabel lblFocus = new JLabel(""); public static void main(String[] args) { new FocusDemo(); Thread t = new Thread() { @Override public void run() { while (true) { EventQueue.invokeLater(new Runnable() { KeyboardFocusManager kfm = new DefaultKeyboardFocusManager(); @Override public void run() { Component c = kfm.getFocusOwner(); String focusInfo = "elsewhere"; if (c == null) { focusInfo = "null"; } else if (c == txtOne) { focusInfo = "txtOne"; } else if (c == txtTwo) { focusInfo = "txtTwo"; } lblFocus.setText(System.currentTimeMillis() + " - Focus owner " + focusInfo); } }); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(System.err); } } } }; t.start(); } private FocusDemo() { EventQueue.invokeLater(new Runnable() { @Override public void run() { JFrame f = new JFrame("Focus bug demo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setPreferredSize(new Dimension(300, 100)); f.setLayout(new GridLayout(3, 1)); NotEmpty validator = new NotEmpty(); txtOne.setInputVerifier(validator); txtTwo.setInputVerifier(validator); f.add(txtOne); f.add(txtTwo); f.add(lblFocus); f.pack(); f.setVisible(true); } }); } private class NotEmpty extends InputVerifier { @Override public boolean verify(JComponent input) { JTextField txtField = (JTextField) input; return (txtField.getText().length() &gt; 0); } } } </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