javaswingjframejtextareajcomponent

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.


Solution

  • 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();