This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes imp layout manager question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "imp layout manager question" Watch "imp layout manager question" New topic
Author

imp layout manager question

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
i have a frame and its layout manager has been set to FlowLayout and has some components added.
question (or rather problem) is that when the Frame appears it is empty. only when resizing is done then all components appear inside it.
what is the reason ? please reply asap.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Have you set the sizes of all the components you're adding to the layout?

Rob


Rob
SCJP 1.4
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
how big is your code? Can you post it here?

Rob
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
import java.awt.*;
class one extends Frame {
public static void main(String args[])
{
Button b1 = new Button("b1");Button b2 = new Button("b2");
one obj = new one();
obj.add(b2);obj.add(b1);
}
one()
{setLayout(new FlowLayout());
setVisible(true);setSize(400,400);
}
}
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
The main problem is you are adding buttons to the Frame AFTER you have made it visible; the objects are added, but until a repaint event gets fired, like when you resize the frame, the newly added buttons won't get drawn.
A couple of other points:
1. keep all the UI code together. You're doing some initialization in main and some in the constructor.
2. Set the size of the frame BEFORE you show it.
3. Set your layout manager BEFORE you add stuff to your frame.
4. Stick with the standard Java naming conventions: your classname needs to start with an initial capital letter
Try this version:


Rob
[ January 31, 2002: Message edited by: Rob Ross ]
 
GeeCON Prague 2014
 
subject: imp layout manager question