Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Please help me with layout

Hello!
I'm getting crazy over all those Layouts (FlowLayout, CardLayout, etc).
I don't know which one to use.

This is how my frame should look:
A single line label
A single line label
A multi line label

OK button

All that might change is the height of the third label.
I want the OK button to be a bit under it, no matter if the third label contains one row or six, and I want the frame to end just below the button...

Could you point me to the right layout to use?

I thought that FlowLayout would be the one, but it shrinks all the controls to their minimum size.

Thanks in advance, Martin
0
martinag
Asked:
martinag
  • 5
  • 4
1 Solution
 
evijayCommented:
let us assume your labels be label1, label2, label3. Here is a piece of code useful for you

Label label1, label2, label3.

frame.setLayout(new BorderLayout());
Panel subPanel = new Panel();
subPanel.setLayout(new GridLayout(2, 1));
subPanel.add(label1);
subPanel.add(label2);
frame.add("North", subPanel);
frame.add("Center", label3);

This should give you the layout you want.

0
 
martinagAuthor Commented:
Almost...
I tried adding the OK button for myself, by adding a new line of code:
this.add("South", ok);
It adds the button to the south, but it adds it VERY southly. Also, its width is the same as the frame's.

If you could come up with another bright idea, it would be great.

Martin
0
 
evijayCommented:
Please create a panel and add the button to that panel and add the panel to the button

Panel tmpPanel = new Panel();
tmpPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
tmpPanel.add(ok);  // add your ok button or any other buttons here

this.add("South", tmpPanel);

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
evijayCommented:
Sorry for the typo, The first sentence should go like this

Please create a panel and add the button to that panel and add the panel to the frame

0
 
martinagAuthor Commented:
All right, everything looks great, but the frame doesn't resize automatically.
I've tried this.setSize(this.getPreferredSize()); but the frame has got the smallest size ever possible.
Could you fix that?

Martin
0
 
evijayCommented:
Just invoke

pack();

after adding all the components and before calling setVisible or show method.


0
 
martinagAuthor Commented:
Looks great!

With your multi-line label and all the help with this, you have definitely deserved to be mentioned at the "Thanks!" dialog (which I think I will manage to design now :-))!
0
 
evijayCommented:
Please not that there can be a lot of enhancements possible to multiline label. As of now, it doesnt support label alignment within the component. It is easy to add that to the code.
0
 
martinagAuthor Commented:
Yes, I have noticed that it isn't perfect, but it is enough for me...

It will be a challenge to get it perfect. Some day...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now