Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 610
seal Avatar asked Sep 12 '13 14:09

seal


2 Answers

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);
like image 76
Ben Dale Avatar answered Oct 31 '22 12:10

Ben Dale


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

like image 34
Enigma Avatar answered Oct 31 '22 13:10

Enigma