seal
seal

Reputation: 1143

Size of Text Area in java

I am writing a code for basic GUI. There i need a Text Area. But i can not make the Text Area in my desirable size. i use setPreferredSize method to set the dimension of the Text Area. But it did not work. I also tried setSize method but did not work also. Here is my written code.

 private void textArea() {
    setTitle("TextArea");
    setSize(700, 500);
    setLayout(new BorderLayout());


    JTextArea textArea = new JTextArea();

    textArea.setPreferredSize(new Dimension(100,100));
    System.out.println(textArea.getSize());

    textArea.setBackground(Color.GREEN);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(false);


    add(textArea,BorderLayout.CENTER);
}

Upvotes: 5

Views: 36132

Answers (2)

Enigma
Enigma

Reputation: 1717

PreferredSize is what it say what it is: a preferred size. The border layout determines the actual size (taking the preferred size into considerations).

See: http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

Consider other layouts to get your desired size. E.G. flowLayout: http://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html

Upvotes: 2

Ben Dale
Ben Dale

Reputation: 2572

setPreferredSize won't always work, plus, it's strongly advised that you use the built in layout managers to deal with any sizing issues.

Try and set the columns and rows on the text area:

new JTextArea(5, 10);

Upvotes: 9

Related Questions