Return to Course Content Home

JFrame, JDialog and JOptionPane

Optional Reading

We will be using the Creating a GUI with JFC/Swing tutorial from Sun as a Reference source. You are not required to read the entire tutorial, I suggest you skim through it though to use a a supplement to what I give you here.

JFrame

JFrame is a primary or application window that appears on the Desktop of whatever Operating System you are using. JFrame actually subclasses the JWindow class, which looks like a JFrame but without any type of borders. (Think of the content of a window, without any controls).

JFrame is actually a somewhat complex container, that you'll typically only use a part of.

JFrame Structure
JFrame structure

Menus are automatically placed in the Menu Bar section. Everything else goes in the Content Pane. It is possible to place things in the "invisible" glass pane, but if you put something there, mouse events will no longer make it to the Content Pane.

It used to be that with a JFrame, you always had to call the getContentPane() method to get a reference to the Content Pane. Then you could add things to the Content Pane. In JDK 1.5+, you can now add things directly to the JFrame (it assumes you are adding to the Content Pane).

Disclaimer: In many examples using JFrames, you typically see the pack() and setVisible() methods used to create the JFrame;

If you look in the API for JFrame, you'll see that there are several constructors. Let's look at an example using the simple constructor taking a String object as an argument. In December 2003, Sun issued a tutorial stating that the following sequence should not be used when creating a JFrame:

The pack() or setVisible() methods create the associated helper for the frame, and the system creates the event dispatch thread. It turns out that the thread may then call listeners while pack and validate() are still processing.
This may generate some event problems in more complex GUIs.

So, the official Sun recommended method is as follows:

final JFrame jf = new JFrame();
  Runnable showFrame = new Runnable() {
  public void run() {
    jf.pack()
    jf.setVisible(true);
  }
};
SwingUtilities.invokeLater(showFrame);

This being said, for our simple examples, doing it the "dangerous" way is acceptable, just remember, it should not be used for any production code!

Before we look as some example code, the table below shows some common methods that JFrame uses.

method
purpose
pack() automatically "packs" the JFrame to be just big enough to hold it's contents
toFront(), toBack() If this Window is visible, brings this Window to the front and may make it the focused Window.
setAlwaysOnTop(boolean) Ensures the JFrame can't be covered by another window
setLocationByPlatform(boolean) Lets the OS place the window on the Desktop, instead of always placing it at the top left.
setDefaultCloseOperation(int)
  • DO_NOTHING_ON_CLOSE - do not do anything - require the program to handle the operation in the windowClosing method of a registered WindowListener object. This is useful for "catching" closing events and preventing them from occurring until a condition is met.
  • HIDE_ON_CLOSE - automatically hide the frame after invoking any registered WindowListener object. This is the default operation.
  • DISPOSE_ON_CLOSE - automatically hide and dispose the frame after invoking any registered WindowListener objects
  • EXIT_ON_CLOSE - Exit the application by way of System.exit. Only use this in applications.
setSize(java.awt.Dimension) Explicitly sets the size of the JFrame.

JFrame Practice

Download and open the Netbeans project swingjframe. If you run the application you will see the following:

swingjframe project output
swingjframe

The application is very simple, it creates three JFrames. The first has no contents and we "forgot" to either call setSize() or pack(). This is a "zero size" JFrame. If you ever see this, it is a warning that you forgot to set the size.

The second window is an empty JFrame, but we called setSize();

The third window is a JFrame with a JPanel. Since the JPanel has a preferred size, we can call pack() to set the size of the JFrame.

Notice that all three JFrames have:

jf1.setLocationByPlatform(true);
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

This tells the OS to place the windows (they all won't come up at the top) and that if you close one window, you'll quit the application.

JDialog

JDialog windows are considered "secondary" windows. They allow a user to “briefly” divert from normal program procedures to carry on a separate conversation with the application in a separate window
You use them for:

JDialogs may be modal or non-modal:

In many ways, JDialogs look a lot like a JFrame. The main difference is that they can be "slaved" to the main application window (JFrame). Because they are secondary, you can't iconify them, you can't maximize/minimize them, and they don't create an icon on your Desktop taskbar. When you create a JDialog, there are constructors that take a JFrame as an argument. If you create a JDialog with a JFrame, the JDialog "disappears" when the JFrame is iconified.

JDialogs have the same internal structure as a JFrame.

JOptionPane

The JOptionPane class was designed to simplify the process of notifying the user of an event. JOptionPane has a lot of methods, but you will see that they are all variations on a central theme. Their appearance is fairly consistent, although the underlying Look & Feel is responsible for final layout.

Within a JOptionPane, there are four basic components

The component appearance and types are typically selected by using different types of JOptionPanes. The figure below shows the typical layout for a JOptionPane:

JOptionPane layout
JOptionPane layout

JOptionPanes are pre-fabricated modal dialog windows for quick user interactions. It is a convenience class.

If you look in the API, it has excellent examples on how to create simple JOptionPanes. Typically, you are letting the user know either a system status or asking a simple question.

You may have noticed that there are a lot of static methods in this class. They are provided so you can quickly create a JOptionPane.

If I wanted to pop up a JOptionPane that asked a Yes or No question. I would use

int answer = JOptionPane.showConfirmDialog(null, "choose one", "choose one", JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION) {
  // then the user answered yes...
} else {
  // then the user answered no...
}

If you really looked at the API, you may have recognized that the code above was copied directly from the JOptionPane page! The documentation is so good on this page, that whenever I want to use a JOptionPane, I just copy code from the API page.

One of the tips of using a JOptionPane is to never pass null as the parent. Many books tell you do to this to "center" the JOptionPane on a screen. There are two good reasons why you don't want to do this.

  1. Without a parent, the JOptionPane appears in the center of the screen, which is not where the user had their attention focused.
  2. If you have multiple monitors, the JOptionPane may be centered in the multiple monitors, causing it to "straddle" the monitors

JOptionPane Practice

Download and load the Netbeans project joptionpane. If you run the application, a window will appear that looks like this.

JOptionPane demo

Clicking on a button brings up different types of JOptionPanes.