0% found this document useful (0 votes)
1 views

Java GUI

Uploaded by

Alieu Keita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java GUI

Uploaded by

Alieu Keita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

GUI: graphic user interface

• 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); }
}
}

setBounds(int x-axis, int y-axis, int width, int height)


method is used in the above example that sets the position
Programming GUI with AWT (By creating the object of Frame class)
import java.awt.*; class DemoMain
class DemoGUI {
{ public static void main(String args[])
DemoGUI()
{
{
DemoGUI dg = new DemoGUI();
Frame f = new Frame("Sample GUI");
f.setBounds(50,50,300,200); }
f.setVisible(true); }
}
}

setBounds(int x-axis, int y-axis, int width, int height)


method is used in the above example that sets the position
Programming GUI with AWT
import java.awt.*; class DemoMain
class DemoGUI
{
{
DemoGUI() public static void main(String args[])
{ {
Frame f = new Frame("Sample GUI");
DemoGUI dg = new DemoGUI();
f.setBounds(50,50,300,300);
TextField t1 = new TextField(); }
t1.setBounds(60, 50, 80, 30); }
Label L1 = new Label("Text1");
L1.setBounds(70, 80, 80, 30);
TextField t2 = new TextField();
t2.setBounds(160, 50, 80, 30);
Label L2 = new Label("Text2");
Adding components
L2.setBounds(170, 80, 80, 30);
Button button = new Button("click Here");
button.setBounds(100, 150, 80, 30);

f.add(button); f.add(t1); f.add(t2); f.add(L1);


f.add(L2);
f.setLayout(null);
f.setVisible(true);
}
}
Programming GUI with SWING
import javax.swing.*; class DemoMain
class DemoGUI
{
{
DemoGUI() public static void main(String args[])
{ {
JFrame f = new JFrame("Sample GUI");
DemoGUI dg = new DemoGUI();
f.setBounds(50,50,300,300);
JTextField t1 = new JTextField(); }
t1.setBounds(60, 50, 80, 30); }
JLabel L1 = new JLabel("Text1");
L1.setBounds(70, 80, 80, 30);
JTextField t2 = new JTextField();
t2.setBounds(160, 50, 80, 30);
JLabel L2 = new JLabel("Text2");
Adding components
L2.setBounds(170, 80, 80, 30);
JButton button = new JButton("click Here");
button.setBounds(100, 150, 120, 30);

f.add(button); f.add(t1); f.add(t2); f.add(L1);


f.add(L2);
f.setLayout(null);
f.setVisible(true);
}
}
Event Handling
• An event can be defined as changing the state of an object or behavior by performing actions.
Actions can be a button click, cursor movement, keypress through keyboard or page scrolling,
etc.
• The java.awt.event package can be used to provide various event classes.
• Event Handling: It is a mechanism to control the events and to decide what should happen
after an event occur.
• To handle the events, Java follows the Delegation Event model.
• Its concept is quite simple: a source generates an event and sends it to one or more listeners.
• Event Sources: A source is an object that generates an event. There are various sources like buttons,
checkboxes, list, menu-item, choice, scrollbar, text components, windows, etc., to generate events.
• Event Listeners: A listener is an object that is notified when an event occurs. Listeners are used for handling
the events generated from the source.
• To perform Event Handling, we need to register the source with the listener.
Delegation Event Model
• The Delegation Event model is defined to handle events in GUI programming languages.
• The GUI programming is inherently event-driven; whenever a user initiates an activity
such as a mouse activity, clicks, scrolling, etc., each is known as an event that is mapped
to a code to respond to functionality to the user. This is known as event handling.
• It defines a standard and compatible mechanism to generate and process events.
• In this model, a source generates an event and forwards it to one or more listeners. The
listener waits until it receives an event. Once it receives the event, it is processed by the
listener and returns it. The UI elements are able to delegate the processing of an event to a
separate function.
Important Event Classes in Java

Event Class Listener Interface Description


An event that indicates that a
component-defined action
ActionEvent ActionListener occurred like a button click or
selecting an item from the
menu-item list.
An event that occurs due to a
KeyEvent KeyListener sequence of keypresses on the
keyboard.
The events that occur due to the
MouseListener &
MouseEvent user interaction with the mouse
MouseMotionListener
(Pointing Device).
An event which indicates
WindowEvent WindowListener whether a window has changed
its status or not.
Listener Interface Methods

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);
}

public void mouseEntered(MouseEvent e)


{ L1.setText("mouseEntered"); }

public void mouseClicked(MouseEvent e)


{ L1.setText("mouseClicked"); }

public void mousePressed(MouseEvent e)


{ L1.setText("mousePressed"); }

public void mouseReleased(MouseEvent e)


{ L1.setText("mouseReleased"); }

public void mouseExited(MouseEvent e)


{ L1.setText("mouseExited"); }
}
Event Handling using MouseMotionListener
import javax.swing.*;
import java.awt.event.*; class DemoMainSwingMouseMotion
class DemoGUI implements MouseMotionListener
{ JTextField t1; JLabel L1;
{
DemoGUI() public static void main(String args[])
{
JFrame f = new JFrame("Sample GUI"); {
f.setBounds(50,50,300,300);
DemoGUI dg = new DemoGUI();
t1 = new JTextField();
t1.setBounds(60, 50, 80, 30);
}
}
L1 = new JLabel("Text1");
L1.setBounds(70, 80, 80, 30);

f.add(t1); f.add(L1);

f.addMouseMotionListener(this);
f.setLayout(null);
f.setVisible(true);
}

public void mouseMoved(MouseEvent e)


{
L1.setText("mouseMoved");
}
public void mouseDragged(MouseEvent e)
{
L1.setText("mouseDragged");
}
}
Adapter classes
• Java adapter classes provide the default implementation of listener interfaces.
Listener Interface Methods Adapter classes
ActionListener actionPerformed()
keyTyped()
KeyAdapter
KeyListener keyPressed()
Using Listener keyReleased()
interface, all its mousePressed() Using Adapter
methods needs mouseClicked() classes, only
MouseListener mouseEntered() MouseAdapter required method
to be
mouseExited() need to be
implemented implemented
mouseReleased()
because adapter
mouseMoved()
MouseMotionListener MouseMotionAdapter classes provide the
mouseDragged()
default
implementation of
windowActivated() listener interfaces.
windowDeactivated()
windowOpened()
WindowListener windowClosed() WindowAdapter
windowClosing()
windowIconified()
windowDeiconified()
Event Handling using MouseAdapter
import javax.swing.*;
import java.awt.event.*; class DemoMainSwingMouseEvent
class MouseAdapter11 extends MouseAdapter
{
{ JTextField t1; JLabel L1; public static void main(String args[])
MouseAdapter11()
{ {
JFrame f = new JFrame("Sample GUI"); f.setBounds(50,50,300,300);
t1 = new JTextField(); t1.setBounds(60, 50, 80, 30); DemoGUI dg = new DemoGUI();
L1 = new JLabel("Text1"); L1.setBounds(70, 80, 80, 30);
}
f.add(t1); f.add(L1); }
f.addMouseListener(this);
f.setLayout(null); f.setVisible(true);
}

public void mouseEntered(MouseEvent e)


{ L1.setText("mouseEntered"); }

You might also like