Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

imp layout manager question

 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you set the sizes of all the components you're adding to the layout?

Rob
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how big is your code? Can you post it here?

Rob
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic