0

I want to have a JFrame. When it's focused (by clicking on it with the mouse, used with Alt+Tab, or somehow else - like by doing it in the program with it's own methods) a specific Component shall be focused directly.

In my Case it would be the following: Clicked on JFrame -> JTextField is focused and the user can write in it directly. Greets, JC

2 Answers2

1

Use a WindowFocusListener and requestFocusInWindow.

myFrame.addWindowFocusListener(new WindowAdapter() {
    @Override
    public void windowGainedFocus(WindowEvent event) {
        someTextField.requestFocusInWindow();
    }
});
VGR
  • 40,506
  • 4
  • 48
  • 63
-1

From https://stackoverflow.com/a/6723316/15093679, you can do:

in = new JTextField(40);
f.addWindowListener( new WindowAdapter() {
    public void windowOpened( WindowEvent e ){
        in.requestFocus();
    }
}); 

Where f is your JFrame and in is your JTextField.

Hope this works.

TheSj
  • 376
  • 1
  • 11