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 ]
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.
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.
posted 18 years ago
I just realized what was wrong with your original code.
[ April 11, 2002: Message edited by: Paul Stevens ]
posted 18 years ago
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 ]
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.
posted 18 years ago
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.