Swing JFrame

In this tutorial you will learn about the Swing JFrame and its application with practical example.

Swing JFrame

The JFrame is an extended version of java.awt.Frame. JFrame works like main window where Lables,Buttons, TextFields components are used to create a GUI. It also adds support of JFC/Swing component architecture.

Table Of Contents

The class declaration of JFrame is like below

JFrame Fields

Fields which are used for creation of JFrame and commonly used are listed below.

protected AccessibleContext accessibleContext : The Accessible Context Property.

static int EXIT_ON_CLOSE : The exit application default window close operation.

protected JRootPane rootPane: The JRootPane instance that manages the content Pane and optional menuBar for this frame, as well the glassPane.

protected boolean rootPaneCheckingEnabled : If true then calls to add and setLayout will be forwarded to the contentPane.

JFrame Constructor

Following constructor are used to create JFrame.

JFrame(): This constructor construct a new frame that is initially invisible.

JFrame(GraphicsConfiguration gc): This constructor is used to creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title.

JFrame(String title): This constructor is used to creates a new initially invisible frame with title.

JFrame(String title, GraphicsConfiguration gc): This constructor is used to creates a Frame with the specified title and the specified GrapicsConfiguration of a screen device.

 

JFrame Methods

Some useful methods are listed below which are used in JFrame.

protected void addImpl(Component comp, Object constraints, int index): It is used to Adds the specified child Component.

protected JRootPane createRootPane() : It is called by the constructor methods to create the default rootPane.

protected void frameInit(): This method is called by the constructor to init the JFrame properly.

void setContentPane(Containe contentPane): This method is used to set contentPane property.

void setIconImage(Image icon): This method is used to sets the image to be displayed as the icon for this window.

static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated): This method is used to provides a hint that newly created JFrame have their Window decoration provided by the current look and feel.

void setJMenuBar(JMenuBar menubar) : This method is used to set JMenuBar for this JFrame.

void setLayeredPane(JLayeredPane layeredPane): This method is used to set the layeredPane Property.

JRootPane getRootPane(): This method is used to return rootPane object for this JFrame.

TransferHandler getTrasferHandler(): It gets tranferHandler Property.

void setDefaultCloseOperation(int operation): This method is used to sets the operation that will happen by default when the user initiates a “close” on this frame .

 

Example :-

A simple example code is written below, for getting understand that how frame creates.

Output :-

Output of this code will look like below image.

image001

In this tutorial we have learn about the Swing JFrame and its application with practical example. I hope you will like this tutorial.