Switching JTextFields by pressing Enter
2 answers
Simple example:
Suppose you have two JTextField
s: textField
andtextField1
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField1.requestFocusInWindow();
}
});
textField1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField.requestFocusInWindow();
}
});
Now when you click Enter
when focused on the first JTextField
, the other will be focused and vice versa.
+5
source to share
Take a look at How to use the focus subsystem
It explains how to set transfer keys for the focus subsystem.
A simple example
public class TestFocusTransfersal {
public static void main(String[] args) {
new TestFocusTransfersal();
}
public TestFocusTransfersal() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField field1 = new JTextField("1", 10);
private JTextField field2 = new JTextField("2", 10);
private JTextField field3 = new JTextField("3", 10);
private JTextField field4 = new JTextField("4", 10);
private JTextField field5 = new JTextField("5", 10);
private JTextField field6 = new JTextField("6", 10);
private JTextField field7 = new JTextField("7", 10);
private JTextField field8 = new JTextField("8", 10);
private JTextField field9 = new JTextField("9", 10);
private final MyOwnFocusTraversalPolicy policy;
public TestPane() {
// Set up enter for focus transfer...
Set forwardKeys = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
Set newForwardKeys = new HashSet(forwardKeys);
newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
newForwardKeys);
setLayout(new GridBagLayout());
add("Field #1: ", field1, 0, 0);
add("Field #2: ", field2, 2, 0);
add("Field #3: ", field3, 4, 0);
add("Field #4: ", field4, 6, 0);
add("Field #5: ", field5, 8, 0);
add("Field #6: ", field6, 2, 1);
add("Field #7: ", field7, 4, 1);
add("Field #8: ", field8, 6, 1);
add("Field #9: ", field9, 8, 1);
policy = new MyOwnFocusTraversalPolicy(
field1,
field6,
field7,
field8,
field9,
field2,
field3,
field4,
field5
);
// You can do this to make life easier, but it may have unexpected
// consequences...
// setFocusCycleRoot(true);
// setFocusTraversalPolicy(policy);
}
@Override
public void addNotify() {
super.addNotify();
// Comment this out if you use focusCycleRoot
SwingUtilities.getWindowAncestor(this).setFocusTraversalPolicy(policy);
}
protected void add(String label, JTextField field, int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = y;
gbc.gridx = x;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.anchor = GridBagConstraints.EAST;
add(new JLabel(label), gbc);
gbc.gridx++;
add(field, gbc);
}
}
public static class MyOwnFocusTraversalPolicy
extends FocusTraversalPolicy {
private List<Component> order;
public MyOwnFocusTraversalPolicy(Component... order) {
this.order = new ArrayList<>(Arrays.asList(order));
}
@Override
public Component getComponentAfter(Container focusCycleRoot,
Component aComponent) {
int idx = (order.indexOf(aComponent) + 1) % order.size();
return order.get(idx);
}
@Override
public Component getComponentBefore(Container focusCycleRoot,
Component aComponent) {
int idx = order.indexOf(aComponent) - 1;
if (idx < 0) {
idx = order.size() - 1;
}
return order.get(idx);
}
@Override
public Component getDefaultComponent(Container focusCycleRoot) {
return order.get(0);
}
@Override
public Component getLastComponent(Container focusCycleRoot) {
return order.size() > 0 ? order.get(order.size()) : null;
}
@Override
public Component getFirstComponent(Container focusCycleRoot) {
return order.get(0);
}
}
}
+5
source to share