Java GUI
Java GUI
• The GUI stands for Graphical User Interface, where a user graphically/visually interacts with the
system.
• Three sets of Java APIs for graphics programming: AWT(Abstract Windowing Toolkit), Swing and
JavaFX.
• AWT API was introduced in JDK 1.0. Most of the AWT UI components have become obsolete and
should be replaced by newer Swing UI components.
• Swing API, a much more comprehensive set of graphics libraries that enhances the AWT, was
introduced as part of Java Foundation Classes (JFC) after the release of JDK 1.1. JFC consists of
Swing, Java2D, Accessibility, Internationalization, and Pluggable Look-and-Feel Support APIs.
JFC has been integrated into core Java since JDK 1.2.
• The JavaFX, which was integrated into JDK 8, was meant to replace Swing. JavaFX was moved
out from the JDK in JDK 11, but still available as a separate module.
Programming GUI with AWT
• Two important and commonly used packages - java.awt and java.awt.event
• The java.awt package contains the core AWT graphics classes:
• GUI Container classes, such as Frame and Panel.
• GUI Component classes, such as Button, TextField, and Label.
• Layout managers, such as FlowLayout, BorderLayout and GridLayout.
• Custom graphics classes, such as Graphics, Color and Font.
• The java.awt.event package supports event handling:
• Event classes, such as ActionEvent, MouseEvent, KeyEvent and WindowEvent.
• Listener Interfaces, such as ActionListener, MouseListener, MouseMotionListener,
KeyListener and WindowListener.
• Event Listener Adapter classes, such as MouseAdapter, KeyAdapter, and WindowAdapter.
AWT Containers and Components
There are two groups of GUI elements:
1. Component: Components are
elementary GUI entities, such as
Button, Label, and TextField. In
Java AWT, there are classes for each
component as shown in diagram. In
order to place every component in
a particular position on a screen,
we need to add them to a
• In the above figure, there are three containers: a Frame and two Panels. container.
• A Frame is the top-level container of an AWT program. A Frame has a title bar
(containing an icon, a title, and the minimize/maximize/close buttons), an optional menu 2. Container: Containers, such as
bar and the content display area. Frame and Panel, are used to hold
• A Panel is a rectangular area used to group related GUI components in a certain layout. components in a specific layout
• In the above figure, the top-level Frame contains two Panels. (such as FlowLayout or
• There are five components: a Label (providing description), a TextField (for users to GridLayout). A container can also
enter text), and three Buttons (for user to trigger certain programmed actions). hold sub-containers.
• In a GUI program, a component must be kept (or added) in a container. You need to identify a container to hold the components.
Every container has a method called add(Component c). A container (say aContainer) can invoke aContainer.add(aComponent) to add
aComponent into itself.
Useful Methods of Component Class
To create simple AWT example, you need a frame. There are two ways to create a GUI using Frame in
AWT.
1. By extending Frame class (inheritance)
2. By creating the object of Frame class (association)
Programming GUI with AWT (By extending Frame class)
import java.awt.*; class DemoMain
class DemoGUI extends Frame {
{ public static void main(String args[])
DemoGUI()
{
{
DemoGUI dg = new DemoGUI();
setTitle("Sample GUI");
setBounds(50,50,300,200); }
setVisible(true); }
}
}
ActionListener actionPerformed()
keyTyped()
KeyListener keyPressed()
keyReleased()
mousePressed()
mouseClicked()
MouseListener mouseEntered()
mouseExited()
mouseReleased()
mouseMoved()
MouseMotionListener mouseDragged()
windowActivated()
windowDeactivated()
windowOpened()
WindowListener windowClosed()
windowClosing()
windowIconified()
windowDeiconified()
Event Handling using ActionListener
import javax.swing.*;
import java.awt.event.*; class DemoMainSwingEVENT
class DemoGUI implements ActionListener
{ JTextField t1, t2; JLabel L3; JButton b1, b2; {
DemoGUI()
{ public static void main(String args[])
JFrame f = new JFrame("Sample GUI");
f.setBounds(50,50,300,300);
{
t1 = new JTextField("0"); t1.setBounds(60, 50, 80, 30); DemoGUI dg = new DemoGUI();
JLabel L1 = new JLabel("Text1"); L1.setBounds(70, 80, 80, 30);
t2 = new JTextField("0"); t2.setBounds(160, 50, 80, 30); }
JLabel L2 = new JLabel("Text2"); L2.setBounds(170, 80, 80, 30);
b1 = new JButton("SUM"); b1.setBounds(100, 150, 70, 30); }
b2 = new JButton("SUB"); b2.setBounds(100, 190, 70, 30);
L3 = new JLabel("0"); L3.setBounds(190, 170, 70, 30);
f.add(b1); f.add(b2); f.add(t1); f.add(t2); f.add(L1); f.add(L2);
f.add(L3);
b1.addActionListener(this); b2.addActionListener(this);
f.setLayout(null); f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int z=0;
if(e.getSource()==b1)
z = x+y;
else if(e.getSource()==b2)
z = x-y;
L3.setText(Integer.toString(z));
}
}
Event Handling using KeyListener
import javax.swing.*;
import java.awt.event.*; class DemoMainSwingKeyEVENT
class DemoGUI implements KeyListener
{ JTextField t1; JLabel L1; {
DemoGUI() public static void main(String args[])
{
JFrame f = new JFrame("Sample GUI"); {
f.setBounds(50,50,300,300);
t1 = new JTextField(); DemoGUI dg = new DemoGUI();
t1.setBounds(60, 50, 80, 30);
L1 = new JLabel("Text1"); }
L1.setBounds(70, 80, 80, 30); }
f.add(t1); f.add(L1);
t1.addKeyListener(this);
f.setLayout(null);
f.setVisible(true);
}
public void keyPressed(KeyEvent e)
{
L1.setText("KeyPressed");
}
public void keyReleased(KeyEvent e)
{
L1.setText("keyReleased");
}
public void keyTyped(KeyEvent e)
{
L1.setText("KeyTyped");
}
}
Event Handling using MouseListener
import javax.swing.*;
import java.awt.event.*; class DemoMainSwingMouseEvent
class DemoGUI implements MouseListener
{ JTextField t1; JLabel L1;
{
DemoGUI() public static void main(String args[])
{
JFrame f = new JFrame("Sample GUI"); f.setBounds(50,50,300,300); {
t1 = new JTextField(); t1.setBounds(60, 50, 80, 30);
L1 = new JLabel("Text1"); L1.setBounds(70, 80, 80, 30); DemoGUI dg = new DemoGUI();
f.add(t1); f.add(L1);
}
}
f.addMouseListener(this);
f.setLayout(null); f.setVisible(true);
}
f.add(t1); f.add(L1);
f.addMouseMotionListener(this);
f.setLayout(null);
f.setVisible(true);
}