Table of contents
1.
Introduction
2.
JFrame class
2.1.
Declaration
2.2.
Constructors
2.3.
Fields
2.4.
Methods
2.4.1.
addImpl(Component comp, Object constraints, int index)Modifier and Type: protected void.The requested child Component is added.
2.4.2.
createRootPane()
2.4.3.
frameInit()
2.4.4.
getAccessibleContext()
2.4.5.
getContentPane()
2.4.6.
getDefaultCloseOperation()
2.4.7.
getGlassPane()
2.4.8.
getGraphics()
2.4.9.
getJMenuBar()
2.4.10.
getLayeredPane()
2.4.11.
getRootPane()
2.4.12.
getTransferHandler()
2.4.13.
isDefaultLookAndFeelDecorated()
2.4.14.
isRootPaneCheckingEnabled()
2.4.15.
paramString()
2.4.16.
processWindowEvent(WindowEvent e)
2.4.17.
setContentPane(Container contentPane)
2.4.18.
setDefaultCloseOperation(int operation)
2.4.19.
setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)
2.4.20.
setGlassPane(Component glassPane)
2.4.21.
setIconImage(Image image)
2.4.22.
setJMenuBar(JMenuBar menubar)
2.4.23.
setLayeredPane(JLayeredPane layeredPane)
2.4.24.
setLayout(LayoutManager manager)
2.4.25.
setRootPane(JRootPane root)
2.4.26.
setRootPaneCheckingEnabled(boolean enabled)
2.4.27.
setTransferHandler(TransferHandler newHandler)
2.4.28.
update(Graphics g)
3.
Creating and Showing Frames
4.
Frequently Asked Questions
4.1.
What is JFrame?
4.2.
What are the constructors of the JFrame class?
4.3.
What is the use of JFrame?
4.4.
Is JFrame Swing or awt?
4.5.
Where is the JFrame window displayed?
5.
Conclusion
Last Updated: Sep 19, 2025

JFrame in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JFrame is the top-level Swing container that renders a screen window. A frame is the foundational window for additional elements like the menu bar, panels, labels, text fields, buttons, etc. JFrame window opens at the beginning of almost all Swing applications.

JFrame is a class in the javax.swing package that java.awt.frame extends. This top-level window has a title bar and border. There are numerous ways to customize the JFrame class. To learn more about JFrame in Java, see this article.

JFrame class

Declaration

public class JFrame extends frame implements WindowConstants, Accessible, RootPaneContainer
You can also try this code with Online Java Compiler
Run Code

Constructors

Constructor

Description

JFrame()Creates a new frame that is originally invisible.
JFrame(String title)Creates a new Frame with the supplied title that is initially invisible.
JFrame(GraphicsConfiguration gc)Creates a Frame with a blank title in the provided GraphicsConfiguration of a screen device.
JFrame(String title, GraphicsConfiguration gc)The supplied GraphicsConfiguration of a screen device is used to create a JFrame with the specified title.

Fields

Field

Modifier and Type

Description

accessibleContextprotected AccessibleContextThe property of the available context.
EXIT_ON_CLOSEstatic intThe default window closure action when exiting an application.
rootPaneprotected JRootPaneThe JRootPane object controls the glassPane, optional menuBar, and contentPane for this frame.
rootPaneCheckingEnabledprotected booleanIf true, the contentPane will receive calls for add and setLayout.

Methods

addImpl(Component comp, Object constraints, int index)
Modifier and Type: protected void.
The requested child Component is added.

createRootPane()

Modifier and Type: protected  JRootPane.
The constructor methods use this method to establish the default rootPane.

frameInit()

Modifier and Type: protected void.
Called by the constructors to properly initialize the JFrame.

getAccessibleContext()

Modifier and Type: AccessibleContext
The AccessibleContext connected to this JFrame is obtained.

getContentPane()

Modifier and Type: Container
This method returns the contentPane object for this frame.

getDefaultCloseOperation()

Modifier and Type: int
Returns the operation when the user clicks the "close" button on this frame.

getGlassPane()

Modifier and Type: Component
This method returns the glassPane object for this frame.

getGraphics()

Modifier and Type: Graphics
Creates a graphical context for this component.

getJMenuBar()

Modifier and Type: JMenuBar
This method returns the menubar that was set on this frame.

getLayeredPane()

Modifier and Type: JLayeredPane
This method returns the layeredPane object for this frame.

getRootPane()

Modifier and Type: JRootPane
This method returns the rootPane object for this frame.

getTransferHandler()

Modifier and Type: TransferHandler
The transferHandler property is returned.

isDefaultLookAndFeelDecorated()

Modifier and Type: static boolean
Returns true if freshly produced JFrames should use the current style and feel for their Window decorations.

isRootPaneCheckingEnabled()

Modifier and Type: protected boolean
Returns whether add and setLayout methods are routed to the contentPane.

paramString()

Modifier and Type: String boolean
This method returns a string representation of this JFrame.

processWindowEvent(WindowEvent e)

Modifier and Type: protected void
This component's window events are processed.

setContentPane(Container contentPane)

Type: void
This method sets the contentPane property.

setDefaultCloseOperation(int operation)

Type: void
Sets the operation that will be performed by default when the user clicks the "close" button on this frame.

setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)

Modifier and Type: static void
This property indicates whether newly formed JFrames should have their Window decorations (such as borders, widgets to close the window, title, etc.) given by the current look and feel.

setGlassPane(Component glassPane)

Type: void
This method sets the glassPane property.

setIconImage(Image image)

Type: void
This method sets the image to be displayed as the icon for this window.

setJMenuBar(JMenuBar menubar)

Type: void
This method sets the menubar for this frame.

setLayeredPane(JLayeredPane layeredPane)

Type: void
This method sets the layeredPane property.

setLayout(LayoutManager manager)

Type: void
This method sets the LayoutManager.

setRootPane(JRootPane root)

Type: protected void
This method sets the rootPane property.

setRootPaneCheckingEnabled(boolean enabled)

Type: protected void
Sets whether add and setLayout calls are directed to the contentPane.

setTransferHandler(TransferHandler newHandler)

Type: void
Sets the transferHandler property, which is a means for allowing data to be transferred into this component.

update(Graphics g)

Type: void
It simply calls for paint (g).

Also check out Swing component in java

Creating and Showing Frames

Here's a snapshot of the FrameDemo example application's straightforward window:

The FrameDemo code below demonstrates how to create and configure a frame:

import javax.swing.*;  
import java.awt.*;
import java.awt.event.*;
public class FrameDemo {  
    public static void main (String[]args){
    //1. Create the frame.
    JFrame frame = new JFrame("FrameDemo");
    //2. Optional: What happens when the frame closes?
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //3. Create components and put them in the frame.
    //...create emptyLabel...
    JLabel emptyLabel = new JLabel("");
    emptyLabel.setPreferredSize(new Dimension(300, 100));
    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
    //4. Size the frame.
    frame.pack();
    //5. Show it.
    frame.setVisible(true);
    }
}
You can also try this code with Online Java Compiler
Run Code

Frequently Asked Questions

What is JFrame?

JFrame is a class in the javax.swing package that java.awt.frame extends. This top-level window has a title bar and border. There are numerous ways to customize the JFrame class. JFrame window opens at the beginning of almost all Swing applications.

What are the constructors of the JFrame class?

There are four constructors of the JFrame class:

  • JFrame()
  • JFrame(GraphicsConfiguration gc)
  • JFrame(String title)
  • JFrame(String title, GraphicsConfiguration gc)

What is the use of JFrame?

A frame is a foundational window for additional elements like the menu bar, panels, labels, text fields, buttons, etc. JFrame window opens at the beginning of almost all Swing applications.

Is JFrame Swing or awt?

JFrame is a class in the javax.swing package that java.awt.frame extends.

Where is the JFrame window displayed?

JFrame can be used to implement the majority of Java Swing applications. A JFrame can be displayed in the top-left corner of a screen by default. Using the setLocationRelativeTo() method of the Window class, we can show the center position of JFrame.

Conclusion

In this blog, we extensively discussed Frames, creating them with the help of the JFrame class with suitable examples. We have addressed every detail related to the JFrame class with syntax, constructors, methods, and examples. 

Recommended Readings:

Live masterclass