• Post Reply Bookmark Topic Watch Topic
  • New Topic

JRootPane & JFrame  RSS feed

 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will be my first post to Java Ranch!

Today I have been playing around with the Swing package and stumbled upon something interesting.

If you instantiate a JFrame and want to place a component like a JButton on it your code will may look similar to:

frame.getContentPane().add(button);

In this line we invoke the method getContentPane() on the reference variable frame. Once the content pane is available we invoke the add() method on the content pane and pass the button reference through to add the JButton object to the content pane.
That logic pretty much seems to sum up why there are two methods here and answers the question I had created.

I went a bit farther and decided to figure out exactly what a content pane is. I found out that when you instantiate a JFrame object ( JFrame frame = new JFrame(); ) you also create what's called a JRootPane which contains a few pieces to the puzzle. The JRootPane holds a glassPane, layeredPane, JMenuBar (optional) and our target the contentPane. The contentPane holds all of our components that we add to it via our code.

I created this post just to see how friendly these forums are wanted to share a bit of new found knowledge I stumbled upon today. It seems weird that when I create a JFrame I also create a JRootPane which has all the pieces I need to create a useful GUI. Isn't Java great?

If anybody else knows of any double method invocation (or whatever it is called) I would love to know about them! Or if you have anything to add!

Have a good one!

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch! You figured that out very well.

Those "double dot" method calls are fairly common and also a bit of a controversy. Some lines of thinking say "never use more than on dot." But a lot of coders will use double and even triple dot method calls as long as it's clear. Think of System.out.println(). It would be tedious to always write this:



But double and triple dot methods calls are harder to debug, so use with care, or even avoid for now.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

There are good reasons for using multiple dots and bad reasons. You can use multiple dots on a StringBuilder like this:-This is probably a good use. You can call things like:-Now that is not so good. People will say that using such code entails the calling code knowing something about the implementation of the Car class or the JFrame class, exposing implementation details is usually a bad thing. They would prefer to say myCar.start(). In fact, if you look through a newer tutorial or JFrame documentation you will find they have allowed you to do exactly that: they have an add method which bypasses the getContentPane call.

Interestingly enough, that is not a void method, so I think you can write myFrame.add(...).add(...).add(...);You can find more about Frames in the Java Tutorials, where you find they actually have four panes.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have found the four panes already.
 
Stephan van Hulst
Saloon Keeper
Posts: 7983
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Cielo!

JRootPane doesn't actually contain all the pieces! A JRootPane is just a lightweight container that holds most of the stuff in your GUI, but can't exist on its own. It needs a place to live. These places are called Top Level Components. The 4 top level components of Swing are JFrame, JWindow, JDialog and JApplet. They form the boundary between the Operating System's windowing system, and light-weight Swing operations. JRootPane doesn't know how to speak with the OS; the top level components translate. When you resize a JFrame, the frame will tell the root pane that everything needs to resize.

Another difference is that a JRootPane only contains all the "insides" of your GUI. JFrame adds the application frame (with the application menu and the close button etc.).

There is one more component that contains a root pane, that's JInternalFrame. JInternalFrame is like a JFrame, except it's lightweight and can only exist within a JDesktopPane (which is a component you can place within a JFrame).
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both!

I agree that it would be extremely tedious to write that PrintStream over and over again. If I did have to, I would definitely use an import of java.io.*
When I looked over frame.getContentPane().add(button) the first thing that came to mind with two dot operators was System.out.println().
However, after thinking about it I noticed that with the frame.getContentPane().add(button) the order was Reference.Method.Method and with System.out.println() the order was Class.Reference.Method.
Still though, the two operators are still there, just representing different things.

I really like that example of the StringBuilder. It almost seems that it could go on forever. In addition, for skipping over the getContentPane() method I did notice that in the interface RootPaneContainer the API tells us they did it just for convenience. I noticed that before I posted this but I still wanted to explore all boundaries. Thanks again!
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the appending to a StringBuilder can go on for as long as you like, within the 64k size limit for a method.
You can get an import of PrintStream (probably better to use import java.io.PrintStream;), but that won't get you that particular PrintStream object used by the System class. System actually uses two PrintStream objects. For that you would have to use import static java.lang.System.out; (or .*;) and write out.println(...); but that hasn't gained popularity and many people don't like such imports.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!