0

I'm trying to make pressing the enter key go to the next text field (TextField2), instead of (by default) it pressing the ok from JOptionPane's OK_CANCEL_OPTION.

   JTextField textField1 = new JTextField("");
   JTextField textField2 = new JTextField("");

   JPanel panel = new JPanel(new GridLayout(0,1));

   panel.add(new JLabel ("Check"));
   panel.add(textField1);
   panel.add(new JLabel ("Cash" ));
   panel.add(textField2);
   textField1.addAncestorListener(new SetFocus());
   textField1.addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
           textField2.addAncestorListener(new SetFocus());
        }
    }

});

   JOptionPane.showConfirmDialog(null, panel, "Deposit", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
amzy
  • 1

1 Answers1

0

Use textfield2.requestFocus() instead of adding an ancestor listener.

Ryan S.
  • 134
  • 2
  • 9