Swing in Java
Swing in Java
Swing in Java
Applet
An applet is a special kind of Java program that runs in a Java enabled browser.
This is the first Java program that can run over the network using the browser. Applet is
typically embedded inside a web page and runs in the browser.
In other words, we can say that Applets are small Java applications that can be accessed
on an Internet server, transported over Internet, and can be automatically installed and run
as a part of a web document.
To create an applet, a class must class extends java.applet.Applet class.
An Applet class does not have any main() method.
It is viewed using JVM. The JVM can use either a plug-in of the Web browser or a separate
runtime environment to run an applet application.
JVM creates an instance of the applet class and invokes init() method to initialize an
Applet.
Note: Java Applet is deprecated since Java 9. It means Applet API is no longer considered important.
Lifecycle of Java Applet
Following are the stages in Applet
An Applet Skeleton
Most applets override these four methods. These four methods form Applet lifecycle.
init() : init() is the first method to be called. This is where variable is initialized. This method
is called only once during the runtime of applet.
start() : start() method is called after init(). This method is called to restart an applet after it
has been stopped.
stop() : stop() method is called to suspend thread that does not need to run when applet is not
visible.
destroy() : destroy() method is called when your applet needs to be removed completely from
memory.
Note: The stop() method is always called before destroy() method.
Swing
Swing in java is part of Java foundation class which is lightweight and platform
independent.
It is used for creating window based applications.
It includes components like button, scroll bar, text field etc. Putting together all these
components makes a graphical user interface.
It is a part of the JFC (Java Foundation Classes).
It is built on top of the AWT API and entirely written in java.
Container Class
Any class which has other components in it is called as a container class. For building
GUI applications at least one container class is necessary.
Following are the three types of container classes:
1. Panel – It is used to organize components on to a window
2. Frame – A fully functioning window with icons and titles
3. Dialog – It is like a pop up window but not fully functional like the frame
Explanation: All the components in swing like JButton, JComboBox, JList, JLabel are
inherited from the JComponent class which can be added to the container classes. Containers
are the windows like frame and dialog boxes. Basic swing components are the building blocks
of any gui application.
JButton Class
It is used to create a labelled button. Using the ActionListener it will result in some action
when the button is pushed. It inherits the AbstractButton class and is platform independent.
import javax.swing.*;
public class swing1
{
public static void main(String args[])
{
// Creates a new, initially invisible Frame with the specified title.
JFrame f = new JFrame("example");
// It creates a button with the specified text.
JButton b = new JButton("LogIn");
// x axis, y axis, width, height
b.setBounds(40,90,85,20);
f.add(b);
f.setSize(300,300); // 400 width and 500 height
f.setLayout(null);
f.setVisible(true);
}
}
Note : Java (Swing/AWT) uses something called LayoutManagers to place UI components on the screen. These
LayoutManagers are responsible to render component such as TextField, CheckBox, etc in a predefined manner
on your Window.
JTextField Class
It inherits the JTextComponent class and it is used to allow editing of single line text.
import javax.swing.*;
public class swing2
{
public static void main(String args[])
{
JFrame a = new JFrame("example");
JTextField b = new JTextField("Username");
b.setBounds(50,100,200,30);
a.add(b);
a.setSize(300,300);
a.setLayout(null);
a.setVisible(true);
}
}
JPanel Class
The JPanel is a simplest container class. It provides space in which an application can attach
any other component. It inherits the JComponents class.It doesn't have title bar.
import java.awt.*;
import javax.swing.*;
public class swingpenal
{
swingpenal()
{
JFrame a = new JFrame("example");
JPanel p = new JPanel();
p.setBounds(70,70,200,200);
p.setBackground(Color.gray);
JButton b = new JButton("Submit");
b.setBounds(190,50,80,40);
b.setBackground(Color.yellow);
p.add(b);
a.add(p);
a.setSize(400,400);
a.setLayout(null);
a.setVisible(true);
}
public static void main(String args[])
{
new swingpenal();
}
}
JLabel Class
It is used for placing text in a container. It also inherits JComponent class.
import javax.swing.*;
public class swinglbl
{
public static void main(String args[])
{
JFrame a = new JFrame("Login");
JLabel b1;
b1 = new JLabel("Usenane");
b1.setBounds(20,20,50,20);
a.add(b1);
a.add(t);
a.setSize(400,400);
a.setLayout(null);
a.setVisible(true);
}
}
JComboBox Class
It inherits the JComponent class and is used to show pop up menu of choices.
import javax.swing.*;
public class swingcmb
{
JFrame a;
swingcmb()
{
a = new JFrame("example");
String courses[] = {"core java","advance java", "java
servlet"};
JComboBox c = new JComboBox(courses);
c.setBounds(40,40,90,20);
a.add(c);
a.setSize(400,400);
a.setLayout(null);
a.setVisible(true);
}
public static void main(String args[])
{
new swingcmb();
}
}
JMenu Class
It inherits the JMenuItem class, and is a pull down menu component which is
displayed from the menu bar.
import javax.swing.*;
class swingmenu
{
JMenu menu1,menu2;
JMenuItem a1,a2;
swingmenu()
{
JFrame a = new JFrame("Example");
menu1 = new JMenu("File");
menu2 = new JMenu("Edit");
JMenuBar m1 = new JMenuBar();
a1 = new JMenuItem("New");
a2 = new JMenuItem("Save");
menu1.add(a1);
menu1.add(a2);
m1.add(menu1);
m1.add(menu2);
a.setJMenuBar(m1);
a.setSize(400,400);
a.setLayout(null);
a.setVisible(true);
}
public static void main(String args[])
{
new swingmenu();
}
}
Java LayoutManagers
The LayoutManagers are used to arrange components in a particular manner. The Java
LayoutManagers facilitates us to control the positioning and size of the components in GUI
forms. LayoutManager is an interface that is implemented by all the classes of layout managers.
There are the following classes that represent the layout managers:
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.
FlowLayout
The FlowLayout arranges the components in a directional flow, either from left to right or
from right to left. Normally all components are set to one row, according to the order of
different components. If all components can not be fit into one row, it will start a new row and
fit the rest in.
Example:
import javax.swing.*;
import java.awt.FlowLayout;
frame.setLayout(new FlowLayout());
frame.add(jb1);
frame.add(jb2);
frame.add(jb3);
jb1.setAlignmentX(Component.LEFT_ALIGNMENT);
// The pack() method is defined in Window class in Java and it sizes the
frame so that all its contents are at or above their preferred sizes.
frame.pack();
frame.setVisible(true);
}
Java BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south, east, west,
and center. Each region (area) may contain one component only. It is the default layout of a
frame or window. The BorderLayout provides five constants for each region:
public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER
import java.awt.*;
import javax.swing.*;
// creating buttons
JButton b1 = new JButton("NORTH"); // the button will be labeled as NORTH
JButton b2 = new JButton("SOUTH"); // the button will be labeled as SOUTH
JButton b3 = new JButton("EAST"); // the button will be labeled as EAST
JButton b4 = new JButton("WEST"); // the button will be labeled as WEST
JButton b5 = new JButton("CENTER"); // the button will be labeled as CENTER
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new border();
}
}
GridLayout manager
The GridLayout manager is used to lay out the components in a rectangle grid, which has
been divided into equal-sized rectangles and one component is placed in each rectangle. It
can constructed with following methods:
GridLayout(): construct a grid layout with one column per component in a single
row.
GridLayout(int row, int col): construct a grid layout with specified numbers of rows
and columns.
GridLayout(int row, int col, int hgap, int vgap): construct a grid layout with
specified rows, columns and gaps between components.
With the following code, we can create a grid layout object with two rows, three columns.
Similarly, we can change the order of two and three to create three rows, two columns grid
layout object.
import javax.swing.*;
import java.awt.GridLayout;