Reputation: 17183
I have a JTextArea inside a JPanel. The JPanel is set to the default FlowLayout.
I assigned to the JTextArea a size through the constructor: new JTextArea(50,50)
.
However, not only the size I specified is ignored once it's out of a certain range (for example, if I set the size to a value larger than 40*40, the program starts to ignore what I say and instead set the JTextArea to some arbitrary size) - the size also changes as I type. The JTextArea resizes itself if I type inside it more than it can contain.
I did:
textArea.setLineWrap(true);
Didn't solve this.
How can I set a fixed size for the JTextArea?
Upvotes: 2
Views: 22117
Reputation: 6223
If you want to have fixed size, you have to put it inside a box like JScrollPane:
JScrollPane jsp = new JScrollPane(textarea);
if you want the text area to fit maximum number of letters:
textarea.setColumns(100);
Upvotes: 6
Reputation: 499
If you include it in a JScrollPane that might solve your problem :
JTextArea textArea = new JTextArea(50, 50);
JScrollPane scrollPane = new JScrollPane( textArea );
You might also have to change you Layout, because by default I think it use the BorderLayout
that expands your components. Or put it in PAGE_START
like this :
add(new JScrollPane(new JTextArea(50, 50)), BorderLayout.PAGE_START);
Upvotes: 8