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);
}
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With