Solved

Please help me with layout

Posted on 1998-06-21
9
184 Views
Last Modified: 2010-03-30
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
Comment
Question by:martinag
  • 5
  • 4
9 Comments
 
LVL 4

Accepted Solution

by:
evijay earned 50 total points
ID: 1223467
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
 
LVL 4

Author Comment

by:martinag
ID: 1223468
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
 
LVL 4

Expert Comment

by:evijay
ID: 1223469
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
 
LVL 4

Expert Comment

by:evijay
ID: 1223470
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Author Comment

by:martinag
ID: 1223471
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
 
LVL 4

Expert Comment

by:evijay
ID: 1223472
Just invoke

pack();

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


0
 
LVL 4

Author Comment

by:martinag
ID: 1223473
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
 
LVL 4

Expert Comment

by:evijay
ID: 1223474
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
 
LVL 4

Author Comment

by:martinag
ID: 1223475
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now