Table of content:

    1. Creating a JFrame window
    2. Setting layout manager
    3. Adding child components
    4. Specifying window closing behavior
    5. Showing the frame on screen
    6. Handling window events
    7. Customizing JFrame’s appearance
    8. JFrame demo program
JFrame is a Swing’s top-level container that renders a window on screen. A frame is a base window on which other components rely, such as menu bar, panels, labels, text fields, buttons, etc. Almost every Swing application starts with JFrame window. This article provides a summary of common practices when using JFrame in Swing development.

 

1. Creating a JFrame window

JFrame frame = new JFrame("Demo program for JFrame");
 

In the code above we create a frame window entitled “Demo program for JFrame”.

2. Setting layout manager

The default layout of the frame is BorderLayout, we can set another layout like this:

frame.setLayout(new GridBagLayout());
 

Or:

frame.setLayout(new GridLayout());
 



Or:

frame.setLayout(new CardLayout());
 

Or:

frame.setLayout(new FlowLayout());
 

Or using absolute layout:

frame.setLayout(null);
 

NOTE: the call setLayout(layout) is equivalent to this call:

frame.getContentPane().setLayout(layout);
  

3. Adding child components to JFrame

The method setJMenuBar(JMenuBar) is used to add a menu bar to the frame. The following example code adds a menu bar with a File > Exit menu:

JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenuItem menuItemExit = new JMenuItem("Exit");
menuFile.add(menuItemExit);

menuBar.add(menuFile);

// adds menu bar to the frame
frame.setJMenuBar(menuBar);
 

Image:

set menu bar for frame

  

4. Specifying window closing behavior for JFrame

We can specify which action will be executed when the user clicks on the frame’s close button:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 

5. Showing JFrame on screen

frame.setAlwaysOnTop(true); 
 

6. Handling window events for JFrame

The interface java.awt.event.WindowListener defines various window events to which we can listen if interested. The method addWindowListener(WindowListener) of JFrame class is used to add a window listener for the frame.

In case we want to listen to only one (or more, but not all) events, we can create a listener class that extends from the WindowAdapter class and override only the interested event methods:

class MyWindowListener extends WindowAdapter {
	// overrides only one method:
	public void windowClosing(WindowEvent event) {
		System.out.println("About to close the window");
	}
}
 

Then add this listener for the frame as follows:

MyWindowListener listener = new MyWindowListener();
frame.addWindowListener(listener);
 

Or we can use anonymous class syntax like this:

addWindowListener(new WindowAdapter() {
	public void windowClosing(WindowEvent event) {
		System.out.println("About to close the window");
	}
});
 

7. Customizing JFrame’s appearance

frame.setUndecorated(true);
 

If the frame is undecorated, its border, title bar and window buttons are all removed, only keep its content pane visible.

 

8. JFrame demo program

To demonstrate the techniques mentioned in this article and for your reference, we create a Swing program looks like the following:

jframe demo program

On clicking the close button, a confirm message dialog appears:

confirm message dialog

If “Yes” is selected, the program exits, otherwise the frame remains visible on screen.

You can download this demo program’s source code and executable jar file in the attachments section.

 

Other Java Swing Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (SwingJFrameDemo.zip)SwingJFrameDemo.zip[Source code and runnable jar file]10 kB