0

Is it possible to change the focus from a jFormattedTextField to a JDateChooser?

I've tried the following:

jFormattedTextField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e){
            jDateChooser.requestFocus();
        }
});

But it doesn't work. The cursor disappears when you press Tab or Enter.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    "Because the focus behavior of this method is platform-dependent, developers are strongly encouraged to use `requestFocusInWindow()` when possible."—[`Component`](http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html). – trashgod Nov 02 '12 at 17:46

2 Answers2

1

requestFocusInWindow() solved the issue. Thanks!

jFormattedTextField.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e){
        jDateChooser.requestFocusInWindow();
    }
});
0

Better way

jDateChooser.getDateEditor().getUiComponent().requestFocusInWindow();
Adrian
  • 655
  • 6
  • 10