Which Java text component should I be using to get it to resize to fit the frame as it is resized?
I have a JTabbedPane and a JTextArea, I want the Textareas/Panes to resize to fit the window when the window is resized. How do I do this? I'm also fuzzy on if it's the right text component to use.
Really Mudd开发者_运维技巧led.
EDIT: I was setting explicit size as a dumb dumb. I've got it all worked out. What I did is set the viewport of a JScrollPane as the JTextArea and remove all explicit sizing.
Thanks chaps!
You can use whatever components you like. All JComponents
can resize with a resizing parent container. You simply need to choose the correct LayoutManager
.
Depending on how you want everything set up, I assume you will want to add your JTabbedPane to your JFrame
like so:
JTabbedPane jtp;
JFrame frame;
// JFrame's default layour is border layout.
frame.add(jtp, BorderLayout.CENTER);
This will cause the JTabbed
pane to take up all of the space of the JFrame
.
You will also need to set your JTabbedPane
's layout manager to BorderLayout
, and also any tab you add to the JTabbedPane will need to have a BorderLayout
.
To set the layout of your components, do:
JComponent anyComponent;
anyComponent.setLayout(new BorderLayout());
Have a look at the Java LayoutManager tutorials for more information.
I should mention that the reason I suggest BorderLayout
is because any component added to the CENTER
of a BorderLayout will automatically take up any space not taken up by any of the borders. And, since you aren't adding anything to any of the other borders (NORTH, SOUTH, EAST, WEST) the component in the center will take up the entire space.
Every JComponent is resized automatically when the container it resides in, is resized.
It's not a matter of the right component but more a matter of which LayoutManager to use. When adding a Component to a container, you can set the minimum size, default size and maximum size.
On resizing the container, the component will scale automatically until reaching the desired maximum size.
If you need code snippets, tell me and I will fire up my IDE later.
cheers
EDIT: jjnguy beat me to it.. nvm then :p
精彩评论