Java Swing Tutorial

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Java Swing Tutorial

Java is a part of Java Foundation Classes (JFC) that is used to create window-based
applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely
written in java.
Unlike AWT, Java Swing provides platform-independent and lightweight components.
The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
Difference between AWT and Swing
There are many differences between java awt and swing that are given below.
No.

Java AWT

Java Swing

1)

AWT
components
dependent.

are platform-

2)

AWT components are heavyweight.

Swing components arelightweight.

3)

AWT doesn't support pluggable look


and feel.

Swing supports pluggable look and


feel.

4)

AWT
provides less
Swing.

Swing
provides more
powerful
components such as tables, lists,
scrollpanes, colorchooser, tabbedpane
etc.

5)

AWT doesn't follows MVC(Model View


Controller) where model represents data,
view
represents
presentation
and
controller acts as an interface between
model and view.

components than

Java swing components areplatformindependent.

Swing follows MVC.

What is JFC
The Java Foundation Classes (JFC) are a set of GUI components which simplify the
development of desktop applications.

Hierarchy of Java Swing classes


The hierarchy of java swing API is given below.

Commonly used Methods of Component class


The methods of Component class are widely used in java swing that are given below.
Method

Description

public void add(Component c)

add a component on another component.

public void setSize(int width,int height)

sets size of the component.

public void setLayout(LayoutManager m)

sets
the
layout
component.

public void setVisible(boolean b)

sets the visibility of the component. It is by


default false.

manager

for

the

Java Swing Examples


There are two ways to create a frame:
By creating the object of Frame class (association)
By extending Frame class (inheritance)
We can write the code of swing inside the main(), constructor or any other method.

Simple Java Swing Example


Let's see a simple swing example where we are creating one button and adding it on the
JFrame object inside the main() method.
File: FirstSwingExample.java
import javax.swing.*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
JButton b=new JButton("click");//creating instance of JButton
b.setBounds(130,100,100, 40);//x axis, y axis, width, height
f.add(b);//adding button in JFrame

f.setSize(400,500);//400 width and 500 height


f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}

Containers
Containers are integral part of SWING GUI components. A container provides a space
where a component can be located. A Container in AWT is a component itself and it
adds the capability to add component to itself. Following are noticable points to be
considered.
Sub classes of Container are called as Container. For example JPanel, JFrame and
JWindow.
Container can add only Component to itself.
A default layout is present in each container which can be overridden using
setLayout method.
SWING Containers:
Following is the list of commonly used containers while designed GUI using SWING.
Sr.
No.

Container & Description

Panel
JPanel is the simplest container. It provides space in which any
other component can be placed, including other panels.

Frame
A JFrame is a top-level window with a title and a border

Window
A JWindow object is a top-level window with no borders and no
menubar.

Top-level Containers are: JFRame, JWindow and JDialog


Light Weight Container: JPanel
JDialog
A Dialog window is an independent subwindow meant to carry temporary notice apart
from the main Swing Application Window. Most Dialogs present an error message or

warning to a user, but Dialogs can present images, directory trees, or just about
anything compatible with the main Swing Application that manages them.
For convenience, several Swing component classes can directly instantiate and
display dialogs. To create simple, standard dialogs, you use the JOptionPane class.
The ProgressMonitor class can put up a dialog that shows the progress of an operation.
Two other classes, JColorChooser and JFileChooser, also supply standard dialogs. To
bring up a print dialog, you can use the Printing API. To create a custom dialog, use
the JDialog class directly.
The code for simple dialogs can be minimal. For example, here is an informational
dialog:

Here is the code that creates and shows it:


JOptionPane.showMessageDialog(frame,
green.");

"Eggs

are

not

supposed

to

be

JPanel:
The class JPanel is a generic lightweight container.
Class constructors
S.N.

Constructor & Description

JPanel()
Creates a new JPanel with a double buffer and a flow layout.

JPanel(boolean isDoubleBuffered)
Creates a new JPanel with FlowLayout and the specified buffering strategy.

JPanel(LayoutManager layout)
Create a new buffered JPanel with the specified layout manager.

JPanel(LayoutManager layout, boolean isDoubleBuffered)


Creates a new JPanel with the specified layout manager and buffering strategy.

Class methods
S.N.

Method & Description

AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JPanel.

PanelUI getUI()
Returns the look and feel (L&F) object that renders this component.

String getUIClassID()
Returns a string that specifies the name of the L&F class that renders this component.

protected String paramString()


Returns a string representation of this JPanel.

void setUI(PanelUI ui)


Sets the look and feel (L&F) object that renders this component.

void updateUI()
Resets the UI property with a value from the current look and feel.

You might also like