Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

when to use frame.getComptentPanel() vs frame w/o getContentPanel()  RSS feed

 
Ted Schrey
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a basic question to try to understand the getContentPane() method...

First, if I create a method to add components to my outer frame:


I am of the understanding that I call it using (in a UserInterface class which implements Runnable)


But it seems to work just fine if I just use (without the .getContentPane())....


I did read Oracle on it, and they indicate that you don't get all the benefits of the ContentPane (class) unless you use the method and will only get the default layout, etc. in a JFrame.... I think I see that, though if I don't change the layout of the JFrame, does it matter?


Second, I guess this question is separate, but I am asking both at same time because I think they are related enough to help understanding.. In that same createComponents above, now, to add the KeyListener, it gets added directly to the JFrame, NOT the component (frame.getContentPane()). Why now do we go directly the frame?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted

Have you read this section of the JFrame API? (emphasis added)
As a convenience, the add, remove, and setLayout methods of this class are overridden, so that they delegate calls to the corresponding methods of the ContentPane.


Ted Schrey wrote:... to add the KeyListener

Swing was designed to work with Key Bindings. It's rare that a KeyListener is the best choice.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!