This week's giveaway is in the Threads forum.
We're giving away four copies of Java Concurrency Live Lessons and have Doug Schmidt on-line!
See this thread for details.
Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Issue with setSize() and pack()  RSS feed

 
Vidya Ram
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm very new to JAVA... I just wrote an applet whose init method is as follows:
public void init()
{
f = new Frame("INIT");
f.setSize(400,400);
f.add(this); // this is to add frame to applet....
f.pack();
f.show();
}
When I load this applet in browser or I call init() from main() to just run this program from command prompt mode, the frame that pops will be shrinked (I mean, its size is not getting set according to my setSize(). If I remove pack(), then it works fine. Could anyone please explain why is this happening?
I need to call pack() to add a canvas to the frame, otherwise, canvas will not appear. I'm confused !!!
Thanks for the help in advance,
Vidya.
[ July 31, 2003: Message edited by: Vidya Ram ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pack tells the container to layout its contents and resize itself based on the needs of the children. Some components, like Canvas, for instance, don't really care what size they are, so pack() makes them very small. You don't need to call pack(), though. What you can do is basically this:
  • Create the frame
  • Set the size of the frame
  • Add the components
  • Call validate() on the frame
  • Call setVisible(true) on the frame

  • [ July 31, 2003: Message edited by: Ernest Friedman-Hill ]
     
    Vidya Ram
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Ernest.
    [ July 31, 2003: Message edited by: Vidya Ram ]
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!