BT93
BT93

Reputation: 339

Why can't I type in a JTextArea unless I resize the window?

I've placed a JTextArea within a JFrame and come across an issue where I can only type within my JTextArea unless I resize the window. How can I get JTextArea to let me type as soon as the window runs without having to resize it?

public class Frame extends JFrame{

    public Note() {
        this.setSize(new Dimension(1000, 1000));
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public JScrollPane createContent(){
        JTextArea textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea);
        this.add(scrollPane, BorderLayout.CENTER);
        return null;
    }
    public static void main(String[] args) {
        new Note();
        Note mainWindow = new Note();
    }
}

Upvotes: 0

Views: 170

Answers (1)

Vishal Gajera
Vishal Gajera

Reputation: 4207

public class Note extends JFrame{

private static final long serialVersionUID = 1L;

public Note() {
    createContent(); // add this line into your code.
    int x = 400;
    int y = 300;
    this.setSize(new Dimension(x, y));
    this.setTitle("Post-It Note");
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public JScrollPane createContent(){
    Color textAreaColor = new Color(248, 247, 235);
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setBorder(null);
    textArea.setBackground(textAreaColor);
    scrollPane.setBackground(textAreaColor);
    textArea.setMargin(new Insets(10, 15, 20, 20));
    this.add(scrollPane, BorderLayout.CENTER);
    return null;
}

public static void main(String[] args) {
    new Note();
  //  mainWindow.createContent(); comment this line...
}

}

Note:

into your older code,

new Note(); // this one create 1-frame..
...
// Note mainWindow = new Note(); this one also create another frame so need to comment it
//  mainWindow.createContent(); does not required because already this method called by constructor...

Upvotes: 1

Related Questions