Victor Yin
Victor Yin

Reputation: 23

How to set a JTextArea to be clicked on by default in a JFrame?

When I launch my program, I want to be able to type in the JTextArea by default. Right now, I have to click on the JTextArea before I can start typing in it.

Upvotes: 0

Views: 35

Answers (1)

mcbauer
mcbauer

Reputation: 66

Try invoking requestFocus(); on the text area

If that doesn't help, add a listener to the frame and request the focus upon receiving the window opened event.

i.e.

    JFrame frame = new JFrame();
    frame.setSize(300, 300);
    JTextArea textArea = new JTextArea();
    frame.add(textArea);
    frame.addWindowListener(new WindowAdapter() {
        public void windowOpened(WindowEvent e) {
            textArea.requestFocus();
        }
    });

    textArea.requestFocus();

Upvotes: 1

Related Questions