Reputation: 75
What I'm trying to do is really simple, but I can't find a solution to it.
I have a JTextArea added to a JPanel and I want to JTexArea to resize when I resize the JPanel. I was able to get the JPanel to resize when I resize the JFrame, but it doesn't seem to work the same way for the JTextArea. Any ideas? Here's the code I used.
public HelpPanel( Map<ComponentType, String> compMap ) {
super();
this.componentMap = compMap;
compDescription = new JTextArea();
setSize(300, 300);
JScrollPane scroll = new JScrollPane(compDescription);
add( scroll );
compDescription.setPreferredSize(new Dimension(getSize()));
compDescription.setLineWrap(true); //Wrap the text when it reaches the end of the TextArea.
compDescription.setWrapStyleWord(true); //Wrap at every word rather than every letter.
compDescription.setEditable(false); //Text cannot be edited.
displayDescription();
}
Upvotes: 0
Views: 2299
Reputation: 50041
Change super();
to super(new BorderLayout());
.
JPanel uses FlowLayout by default, which doesn't try to force the size of its components to fill the space. By switching to a BorderLayout, the center component (the scroll pane) will be forced to match the size of the panel.
Upvotes: 1