Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange tab-order with editable JComboBoxes
    primarykey
    data
    text
    <p>I've got a <code>JFrame</code> that looks like this: </p> <p><img src="https://i.stack.imgur.com/aoB6L.png" alt="enter image description here"></p> <p>It's got two <code>JTextField</code>s on it, one <code>JComboBox</code> between them and a <code>JPanel</code> at the bottom (that you can't see). </p> <p>One of the features of the <code>JComboBox</code> is that it can be given a custom editor. These implement the <code>ComboBoxEditor</code> interface. In each of the following three cases, the GUI looks exactly the same, and I would have expected them all to behave exactly the same: </p> <ul> <li>I do not specify a custom editor, and use the default one. </li> <li>I create a custom editor whose editor component is a <code>JTextField</code>.</li> <li>I create a custom editor whose editor component is a <code>JPanel</code> with a <code>JTextField</code> on it (using a <code>BorderLayout</code>). </li> </ul> <p>When the editor for the editable combo box is set to the default, pressing <kbd>Tab</kbd> moves the focus from the top <code>JTextField</code> into the editing area on the <code>JComboBox</code> and then into the other <code>JTextField</code>. If I create a custom editor whose editor component is a <code>JTextField</code> and otherwise does what you would expect, the same thing happens. </p> <p><strong>BUT</strong>, if I instead create a custom editor whose editor component is a <code>JPanel</code> with a <code>JTextField</code> <code>add</code>ed to it, the focus makes one additional stop. If the focus is on the top <code>JTextField</code>, then pressing <kbd>Tab</kbd> moves the focus to the little arrow at the right of the editable combo box before moving into the text area. </p> <p>Why is this happening? The focus never moves on to the <code>JPanel</code> at the bottom of the frame, so why does the presence of a <code>JPanel</code> holding the <code>JTextField</code> affect the tab order on the combo box? </p> <p>The following is an S(-ish)SCCE, which has one text field and all three types of combo box on it: </p> <pre><code>import javax.swing.*; import java.awt.event.*; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; public class ComboBoxTest extends JFrame { private JPanel layoutPanel; private JTextField meaninglessTextField; private JComboBox defaultEditorComboBox; private JComboBox textFieldEditorComboBox; private JComboBox panelEditorComboBox; public ComboBoxTest() { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); layoutPanel = new JPanel(); layoutPanel.setLayout(new BoxLayout(layoutPanel, BoxLayout.Y_AXIS)); meaninglessTextField = new JTextField(); defaultEditorComboBox = new JComboBox(); // Just a default JComboBox. defaultEditorComboBox.setEditable(true); textFieldEditorComboBox = new JComboBox(); textFieldEditorComboBox.setEditable(true); textFieldEditorComboBox.setEditor(new TextFieldEditor()); panelEditorComboBox = new JComboBox(); panelEditorComboBox.setEditable(true); panelEditorComboBox.setEditor(new PanelEditor()); layoutPanel.add(Box.createRigidArea(new Dimension(500,0))); layoutPanel.add(meaninglessTextField); layoutPanel.add(defaultEditorComboBox); layoutPanel.add(textFieldEditorComboBox); layoutPanel.add(panelEditorComboBox); Container contentPane = getContentPane(); contentPane.add(layoutPanel, BorderLayout.CENTER); pack(); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new ComboBoxTest().setVisible(true); } }); } private class PanelEditor extends JPanel implements ComboBoxEditor { public JTextField inputTextField = new JTextField(); public PanelEditor() { setLayout(new BorderLayout()); add(inputTextField, BorderLayout.CENTER); } @Override public String getItem() { return inputTextField.getText(); } @Override public void setItem(Object newText) { if (newText != null) { inputTextField.setText(newText.toString()); } else { inputTextField.setText(""); } } @Override public Component getEditorComponent() { return this; } @Override public void removeActionListener(ActionListener listener) { inputTextField.removeActionListener(listener); } @Override public void addActionListener(ActionListener listener) { inputTextField.addActionListener(listener); } @Override public void selectAll() { inputTextField.selectAll(); } } private class TextFieldEditor extends PanelEditor implements ComboBoxEditor { // The same, except that the editor component is now just the JTextField // rather than the whole panel. public TextFieldEditor() { } @Override public JTextField getEditorComponent() { return inputTextField; } } } </code></pre> <p>Note: this behaviour becomes a problem if I want to add a <code>JLabel</code> to the editor. Then I have to put a <code>JPanel</code> there to hold both the label and the text field. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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