• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java - Swing Chapters

 
Guy Shahar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Swing chapters of this book, an instance of JFrame is created in each GUI class to hold the content of the GUI, and content is then added using, for example, "frame.add(BorderLayout.EAST, button);".

Elsewhere, I have seen GUI classes created as extensions of JFrame, and a Container instance created rather than the type of layout being specified each time something is added.

As far as I can tell, the only difference between the 2 approaches is that the second reduces typing a little. Is there any other reason why one approach might be preferable to the other?

I am very grateful for this book. I am learning Java as my first language (apart from a little Basic when I was a child), and this book seems to cover as much if not more than other books in a way that is easy to understand, and makes sense. It has probably reduced my learning time by at least several months. Also, a great philosophy for the site - to help people learn by encouraging them to experiment themselves. Thank you.
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Guy Shahar:

As far as I can tell, the only difference between the 2 approaches is that the second reduces typing a little. Is there any other reason why one approach might be preferable to the other?


Because your typing less! I guess
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back in the AWT, you added components to the frame directly. With Swing,
the design is more layered. JFrame has-a JRootPane which manages
the content pane for JFrame. When you "add components", they should actually be added to the
content pane. By default, the content pane is an instance of JPanel that has a BorderLayout.

Before version 1.5, JFrame would, by default, give a runtime error when you
directly added components to a JFrame: frame.add(comp) would cause the error message:

Do not use javax.swing.JFrame.add() use javax.swing.JFrame.getContentPane().add() instead

But in 1.5 this was changed. As the java doc reads now:

The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level
containers, a JFrame contains a JRootPane as its only child. The content pane provided by
the root pane should, as a rule, contain all the non-menu components displayed by the JFrame.
This is different from the AWT Frame case. As a conveniance add and its variants, remove and
setLayout have been overridden to forward to the contentPane as necessary. This means you can
write:

frame.add(child);

And the child will be added to the contentPane.

You can also see this in the list of new Swing features in 1.5:http://java.sun.com/j2se/1.5.0/docs/guide/swing/1.5/index.html
(search for bug id 4753342)

As for your question, a lot of programmers create their own content panel -- it's an easy
way to choose a different layout manager. I'm used to not calling add directly on the JFrame --
why should I write code that's not backward compatible?
 
Guy Shahar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff. Very informative.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!