This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes component layout not wrapping inside Panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "component layout not wrapping inside Panel" Watch "component layout not wrapping inside Panel" New topic
Author

component layout not wrapping inside Panel

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I must be missing something... obvious.
Considering:

I would expect (and I'm hoping to achieve) that the two frames displayed would look the same regarding the layout of the TextFields. However, when using the Panel, the TextFields are not wrapping when they exceed the available width of the Panel, and the Panel just widens itself to allow the TextFields to line up one after the other in a single row. Also, the Panel knows that I gave it a width of 300 and a height of 800, but it doesn't seem to care - it just sizes itself (with regards to its display) as it sees fit, which is not how I want it.
So, I apparently don't understand component layout inside of Panels and sizing the Panel. Anybody care to fill me in?
Thank You.
[ April 11, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
A couple of notes:
My ultimate goal is to have a TextField sitting right on top of a TextArea - no matter the size of the Frame (which can be resized). Like this:

The initial layout is straight forward. I just used FlowLayout and added the two components to the Frame. But, when the Frame is resized beyond a certain width, a problem arises - the FlowLayout lines the components up in a row (TextField next to TextArea).
So, I thought I'd just use a Panel of a specified size and add the components to the Panel. Then, if the Frame were resized - so what, the Panel wouldn't be. But, the components aren't wrapping in the Panel.
Now, I could get excessively complex with the whole thing, and implement ComponentListener to check when things are resized and add this to that and resize the other, maybe use an empty Canvas (or six of them), etc. This just seems like overkill for something so simple - a TextField sitting right on top of a TextArea.
Sure, BoxLayout would be easy enough, but the users are pre-Swing. Yes, GridBagLayout could get the job done, but that just seems like overkill. There must be something very simple that I'm just missing.
Thanks for your ideas.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Try BoxLayout and stack them vertically.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
That's a good idea, but as I was saying:
Originally posted by Dirk Schreckmann:
Sure, BoxLayout would be easy enough, but the users are pre-Swing.

By "pre-Swing" I mean, the application can't use anything from the Swing package.
Any ideas?
[ April 10, 2002: Message edited by: Dirk Schreckmann ]
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You mean something like this:
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You may also want to
constraints.weighty = 1;
If you want, the height to take up the rest. You can set insets to add padding around the components.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
I just realized what was wrong with your original code.

Should be:

[ April 11, 2002: Message edited by: Paul Stevens ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Thanks for all the ideas Paul.
Unfortunately, adding the Panel to the center of a BorderLayout just brings us back to square one - when the Frame is resized horizontally beyond a point, the TextField and TextArea line up next to each other. My goal here is to always have the TextField on top of the TextArea.
Using GridBagLayout does get the job done. I'm using this app to teach some Java programming concepts to some young students who are very new to programming. But I just don't think they're ready for it yet. In two weeks.. sure.
I apparently don't understand what a Panel is supposed to do. I thought that a Panel was a container that I could add components to and I could have control over the dimension of the Panel. Apparently, I have no control over the dimension of the Panel. Not only that, but the Panel doesn't seem to care about the available width provided by its container when it's added under FlowLayout conditions - the Panel just widens itself more and more for each component added to it without wrapping the components when the width of the parent container is exceeded.
Perhaps the bottom line here is: a Panel doesn't behave like I thought it did (and like I think it should - I just want to tell it how big I want it to be).
Again, thanks for all of the help Paul.
[ April 11, 2002: Message edited by: Dirk Schreckmann ]
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Another option for this would be to use GridLayout. The TextField could be placed on a Panel FlowLayout.CENTER so it doesn't grow when resized. The TextArea doesn't matter so just place it in the grid directly it will expand with the frame.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Here's what I ended up doing:

which creates something like:

which is good enough for this simple app I'm using. However, I'm very disappointed in Panel for not doing what I want it to do - occupy a space and keep that space and wrap components. Bad Panel.
Thanks for all of the help and ideas Paul.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: component layout not wrapping inside Panel