• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issue with setSize() and pack()

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Ernest.
    [ July 31, 2003: Message edited by: Vidya Ram ]
    reply
      Bookmark Topic Watch Topic
    • New Topic