This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Layout Managers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Layout Managers" Watch "Layout Managers" New topic

Layout Managers

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I have a simple little issue here. I need to display on the screen a replica of a form, so it will consist of Radio Buttons, CheckBoxes, mostly TextFields, a Table, and appropriate Labels for all of them. The problem is I'm not sure exactly how I go about displaying this form so that every component is displayed properly and more importantly, scales properly and reacts well to different L&Fs.
My biggest problem is, for some reason, I don't seem to be able to figure out how to get everything to stay the right size and in the right position in relation to each other. One thing gets too big, another too small, I just want the damn things to stay in EXACT proportion regardless of how big or small the Frame gets or whether or not all the text in the Label fits or anything else. What's the best approach to this? With GridBagLayout I seemed to have a lot of issues with the proportions changing with it trying to make enough room for a Label to display it's full text and things like that, which would be bad enough except then the entire column gets bigger which throws off the spacing on everything that spans that column. I've done okay with SpringLayout but once I get beyond a few rows of items I have all kinds of trouble getting everything to stretch appropriately and stay in the right spot.
agh... I need guidance... help me! *cries*
EDIT: Something that looks more or less like this (incomplete and crap and not working worth a damn as you can probably see)

[ November 20, 2003: Message edited by: Ken Blair ]
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
If I were you, I would section your display into multiple sub-panels. Get each subpanel displaying correctly and reacting well to resizing, then add each sub-panel to a main panel. I definitely would NOT put all those components straight into one panel (you didn't say whether you were doing that or not).

My Java Freeware:<br />MACCC - <a href="" target="_blank" rel="nofollow"></a><br />Nator - <a href="" target="_blank" rel="nofollow"></a>
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Hrm... I'm not sure how I would split that up into sub panels.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
It may sound extreme, but have you considered writing your own layout manager that lays out components based on x,y,width,height (bounding rectangle). It's actually not that difficult. If you send me an email I'll be happy to share such a class that should work. And it's not protected by any license so you can use it and modify it at will ...
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I have considered it but been intimidated by the prospect. I just want each damn item to maintain the exact same size and position in relation to each other no matter how big or small the frame gets and regardless of whether or not each component can fit its text or anything else in it.
Like I said, I have some issues with positioning but the biggest thing is just getting everything to scale accurately. The component on the far right DISAPPEARS instead of the component to the left of it getting smaller so that both can fit, and other such things. I've done pretty well with Java, managed to get it to parse xml files in excess of three million lines, made a nice little widget for displaying, manipulating, and drawing onto images and any number of other things big and small and yet I can't seem to make ONE SIMPLE STUPID FREAKING FORM! I'm about at the end of my wits here.
Maybe I should look into some GUI development software? Suggestions?
When it comes to SpringLayout I don't know what I'm doing wrong. How do you get the components to stretch and shrink right? For example if I have 7 checkboxes and a text field, then I attach each to the top of the container with a spring of 10, then the far west one to the west of the container with a spring of 10, and then each of the others to the east of the one to the west of it with springs of 0, they don't stretch or shrink! Even if I change the minimum, maximum, and preferred sizes in any number of ways they don't seem to want to do it. Now, if I attach another spring to the east edge of the container from the east edge of the textfield (the rightmost component) it does. However, if I then go down and start making another row of components they don't stretch, and if I attach a spring to the end of the one on the east to the east of the container like I did before then THOSE stretch and the top ones don't! argh... I must be looking at Swing all wrong or something.
[ November 21, 2003: Message edited by: Ken Blair ]
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
>Hrm... I'm not sure how I would split that up into sub panels.
I see MANY ways to split it into sub-panels.
"Program type code" can be one.
you can group some of the items on the right into a subpanl
you can group some of the items on the left into a subpanel
Diagnosis or nature of illness can be a subpanl.
Basically, just group together items that are similar or related. Figure out what layout works for that set of components and set the panel containing those to that layout. You can also create borders around each separate panel (I'm partial to EtchedBorders) - this gives it a nice finished look (IMHO)
As an example, take a look at a screenshot of one of my apps:
There are 4 panels on that page - 3 sub-panels (email, monitoring, and connection log) and one main panel holding the 3 sub-panels. Each sub-panel may have a diferent layout to make that panel look right. Then, the main parent panel can have another. By doing it this way, you pare down your gui into somthing more manageable and you just worry about each sub-panl looking right and when you add them all to the main panel, things SHOULD be OK.
If I were you, I would not put all of those components on-screen at one. I'd come up with another way to present all of the fields - maybe use a cardlayout, or a wizard or something. That's just me though. if you look at the Nator screenshots ( ) I had a number of components and I could have put them all in one panel and shown them all at once, but I think that would have looked cluttered. I chose to separate them into a couple different groups and use a cardlayout. Just something to think about.
[ November 24, 2003: Message edited by: Brian Pipa ]
I agree. Here's the link:
subject: Layout Managers
Similar Threads
Is the left doomed?
Simple CSS puzzle?
2d graphics context from component to image
display contents of a text file in a new window
How to change the button size dynamically ?