SWING: Containers JFrame Class Cheat Sheet

Following is the declaration for javax.swing.JFrame class

public class JFrame
   extends Frame
      implements WindowConstants, Accessible, RootPaneContainer

# Field

Following are the fields for java.awt.Component class 

protected AccessibleContext accessibleContext  The accessible context property.
static int EXIT_ON_CLOSEThe exit application default window close operation.
protected JRootPane rootPaneThe JRootPane instance that manages the contentPane and optional menuBar for this frame, as well as the glassPane.
protected boolean rootPaneCheckingEnabledIf true then calls to add and setLayout will be forwarded to the contentPane.

# Constructors

JFrame()Constructs a new frame that is initially invisible.
JFrame(GraphicsConfiguration gc)Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title.
JFrame(String title)Creates a new, initially invisible Frame with the specified title.
JFrame(String title, GraphicsConfiguration gc)Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device.

# Methods

protected void addImpl(Component comp, Object constraints, int index)Adds the specified child Component.
protected JRootPane createRootPane()Called by the constructor methods to create the default rootPane.
protected void frameInit()Called by the constructors to init the JFrame properly.
AccessibleContext getAccessibleContext()Gets the AccessibleContext associated with this JFrame.
Container getContentPane()Returns the contentPane object for this frame.
int getDefaultCloseOperation()Returns the operation that occurs when the user initiates a “close” on this frame.
Component getGlassPane()Returns the glassPane object for this frame.
Graphics getGraphics()Creates a graphics context for this component.
JMenuBar getJMenuBar()Returns the menubar set on this frame.
JLayeredPane getLayeredPane()Returns the layeredPane object for this frame.
JRootPane getRootPane()Returns the rootPane object for this frame.
TransferHandler getTransferHandler()Gets the transferHandler property.
static boolean isDefaultLookAndFeelDecorated()Returns true if the newly created JFrames have their Window decorations provided by the current look and feel.
protected boolean isRootPaneCheckingEnabled()Returns whether calls to add and setLayout are forwarded to the contentPane.
protected String paramString()Returns a string representation of this JFrame.
protected void processWindowEvent(WindowEvent e)Processes window events occurring on this component.
void remove(Component comp)Removes the specified component from the container.
void repaint(long time, int x, int y, int width, int height)Repaints the specified rectangle of this component within time milliseconds.
void setContentPane(Container contentPane)Sets the contentPane property.
void setDefaultCloseOperation(int operation)Sets the operation that will happen by default when the user initiates a “close” on this frame.
static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)Provides a hint as to whether or not newly created JFrames should have their Window decorations (such as borders, widgets to close the window, title…) provided by the current look and feel.
void setGlassPane(Component glassPane)Sets the glassPane property.
void setIconImage(Image image)Sets the image to be displayed as the icon for this window.
void setJMenuBar(JMenuBar menubar)Sets the menubar for this frame.
void setLayeredPane(JLayeredPane layeredPane)Sets the layeredPane property.
void setLayout(LayoutManager manager)Sets the LayoutManager.
protected void setRootPane(JRootPane root)Sets the rootPane property.
protected void setRootPaneCheckingEnabled(boolean enabled)Sets whether calls to add and setLayout are forwarded to the contentPane.
void setTransferHandler(TransferHandler newHandler)Sets the transferHandler property, which is a mechanism to support the transfer of data into this component.
void update(Graphics g)Just calls paint(g).

# Inherited

  • java.awt.Frame
  • java.awt.Window
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

# JFrame Example


package com.simplecheatsheet.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingContainerDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   private JLabel msglabel;

   public SwingContainerDemo(){
   public static void main(String[] args){
      SwingContainerDemo  swingContainerDemo = new SwingContainerDemo();  
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      msglabel = new JLabel("Welcome to TutorialsPoint SWING Tutorial.", JLabel.CENTER);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

   private void showJFrameDemo(){
      headerLabel.setText("Container in action: JFrame");   
      final JFrame frame = new JFrame();
      frame.setSize(300, 300);
      frame.setLayout(new FlowLayout());       
      frame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
      JButton okButton = new JButton("Open a Frame");
      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            statusLabel.setText("A Frame shown to the user.");

Leave a Reply

Your email address will not be published. Required fields are marked *