Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lesson: Performing Custom Painting (The Java Tutorials)

 
Mohammed Azeem
Ranch Hand
Posts: 38
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

I'm studying the Lesson on Performing Custom Painting in the Java Tutorials. The lesson is in three steps.

In step 2, a JFrame named f is created. A JPanel object called MyPanel is also created and then added to the frame f.

A method named getPreferredSize is defined to set MyPanel's size but the problem I have with understanding why this method is never invoked anywhere in the code. So how does MyPanel and ultimately the JFrame to which it is attached get their sizes.

I am always impressed by the insights offered in this forum, thank you for you help.

The code listed below:

 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Azeem wrote:A method named getPreferredSize is defined to set MyPanel's size but the problem I have with understanding why this method is never invoked anywhere in the code. So how does MyPanel and ultimately the JFrame to which it is attached get their sizes.

This recent thread may be of interest: When is the preferredSize() method of the Container class called?
 
Mohammed Azeem
Ranch Hand
Posts: 38
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Darryl.

Just to clarify my further reading around this issue: am I correct in thinking that pack() calls the getPreferredSize method automatically in this example?

Thanks.

 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing automatic about it. If you really want to know what goes on under the hood, study the JDK source code which you can find in src.zip in your JDK install directory.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic