CSC444 Java Programming Event Handling
CSC444 Java Programming Event Handling
CSC444 Java Programming Event Handling
FES/FSKM/UiTM/JASIN/MELAKA
Graphical User Interface
(GUI)
Components
defines a screen element used to display information or
allow the user to interact with the program (ie: text
fields, check boxes, radio buttons, sliders, combo boxes,
timers, labels, etc.)
Container is special component used to hold and organize
other components
FES/FSKM/UiTM/JASIN/MELAKA
Graphical User Interface
(GUI)
Event
object that represents some occurrences
often correspond to user actions (button press, key press)
FES/FSKM/UiTM/JASIN/MELAKA
Graphical User Interface
(GUI)
Listener
object that waits for event to occur and responds
FES/FSKM/UiTM/JASIN/MELAKA
Graphical User Interface
(GUI)
In Java, GUI-related classes defined in:
java.awt package (Button)
javax.swing (Jbutton)
FES/FSKM/UiTM/JASIN/MELAKA
Creating GUI in Java
FES/FSKM/UiTM/JASIN/MELAKA
Frame and Panel
Frame
Container used to display GUI-based
import javax.swing.*;
public class MyFrame{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("My Frame");
frame.setSize(500, 300); Width:
frame.setVisible(true); 500
}
}
Height:
FES/FSKM/UiTM/JASIN/MELAKA
300
Panel
Container to hold components
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyPanel1 extends JPanel {
private JTextField myTextField;
private JButton myButton;
private JLabel myLabel;
public MyPanel1() {
myTextField = new JTextField(“Please input value.”);
myButton = new JButton("Click");
myLabel = new JLabel("Hello");
// -- more --
add(myTextField);
add(myButton);
add(myLabel);
setBackground(Color.green);
setPreferredSize(new Dimension(300, 400));
}
}
FES/FSKM/UiTM/JASIN/MELAKA
import javax.swing.JFrame;
public class MyModifiedFrame1 {
public static void main(String[] args) {
JFrame frame = new JFrame("Click");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
MyPanel1 objPanel = new MyPanel1();
frame.getContentPane().add(objPanel);
frame.pack();
frame.setVisible(true);
}
}
FES/FSKM/UiTM/JASIN/MELAKA
Please input value.
FES/FSKM/UiTM/JASIN/MELAKA
GUI Components
Components Description
text fields allows the user to enter typed input from the
keyboard
password allow user to enter typed input from the
field keyboard but hides the characters
text area Provide an area for manipulating multiple
lines of text
check boxes a button that can be toggled on or off using
the mouse (indicates a boolean value is set or
unset)
FES/FSKM/UiTM/JASIN/MELAKA
GUI Components
Components Description
radio buttons used with other radio buttons to provide a set
of mutually exclusive options
combo boxes allows the user to select one of several
options from a “drop down” menu
FES/FSKM/UiTM/JASIN/MELAKA
Example: Clicking button
Action Event generated
Therefore, implement ActionListener
• actionPerformed(ActionEvent)
event event
source event listener
Component Handler
register
FES/FSKM/UiTM/JASIN/MELAKA
Types of Event
User Action Source Object Event Type
Click a button JButton ActionEvent
Click a check box JCheckBox ItemEvent
Click a radio button JRadioButton ActionEvent
Press return on a text field JTextField ActionEvent
Select a new item JComboBox ActionEvent
Window opened, closed, Window WindowEvent
etc.
Mouse pressed, released, Component MouseEvent
etc.
Key released, pressed, Component KeyEvent
etc.
FES/FSKM/UiTM/JASIN/MELAKA
MyPanel1.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyPanel1 extends JPanel implements ActionListener{
private JTextField myTextField;
private JButton myButton;
private JLabel myLabel;
public MyPanel1() {
setLayout(new BorderLayout());
myTextField = new JTextField(4);
myButton = new JButton("Click");
myLabel = new JLabel("Hello");
// -- more --
FES/FSKM/UiTM/JASIN/MELAKA
continue…..
myButton.addActionListener(this);
add(myTextField);
add(myButton);
add(myLabel);
setBackground(Color.yellow);
setPreferredSize(new Dimension(300, 400));
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == myButton) {
System.out.println("h");
myLabel.setText(myTextField.getText());
}
FES/FSKM/UiTM/JASIN/MELAKA
}
continue…..
public static void main(String[] args) {
JFrame frame = new JFrame("Click");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
MyPanel1 objPanel = new MyPanel1();
frame.getContentPane().add(objPanel);
frame.pack();
frame.setVisible(true);
}
}
FES/FSKM/UiTM/JASIN/MELAKA
MyPanel.java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class MyPanel extends JPanel implements ActionListener {
private int counter;
private JButton myButton;
private JLabel myLabel;
MyPanel() {
counter = 0;
myButton = new JButton("Hit!");
myButton.addActionListener(this);
myLabel = new JLabel("" + counter);
// -- more --
FES/FSKM/UiTM/JASIN/MELAKA
continue…..
add(myButton);
add(myLabel);
setPreferredSize(new Dimension(300, 150));
}
public void actionPerformed(ActionEvent e) {
counter++;
myLabel.setText("" + counter);
}
}
FES/FSKM/UiTM/JASIN/MELAKA
MyGUI.java
import javax.swing.*;
class MyGUI {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel objPanel = new MyPanel();
frame.getContentPane().add(objPanel);
frame.setTitle("Button Click Demo");
frame.pack();
frame.setVisible(true);
}
}
FES/FSKM/UiTM/JASIN/MELAKA
Layout Manager
FES/FSKM/UiTM/JASIN/MELAKA
Layout Managers
FES/FSKM/UiTM/JASIN/MELAKA
Layout Manager
Layout Description
Manager
Flow Layout Organizes components from left to right,
starting new rows as necessary
Border Layout Organizes components into five areas
(North, South, East, West, and Center)
Grid Layout Organizes components into a grid of rows
and columns
GridBag Layout Organizes components into a grid of cells,
allowing components to span more than one
cell
Box Layout Organizes components into a single row or
column
FES/FSKM/UiTM/JASIN/MELAKA
LayoutDemo Example
FES/FSKM/UiTM/JASIN/MELAKA
//********************************************************************
// LayoutDemo.java Java Foundations
//
// Demonstrates the use of flow, border, grid, and box layouts.
//********************************************************************
import javax.swing.*;
public class LayoutDemo{
//-----------------------------------------------------------------
// Sets up a frame containing a tabbed pane. The panel on each
// tab demonstrates a different layout manager.
//-----------------------------------------------------------------
public static void main(String[] args){
JFrame frame = new JFrame("Layout Manager Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(tp);
frame.pack();
frame.setVisible(true);
} FES/FSKM/UiTM/JASIN/MELAKA
}
//********************************************************************
// IntroPanel.java Java Foundations
//
// Represents the introduction panel for the LayoutDemo program.
//********************************************************************
import java.awt.*;
import javax.swing.*;
public class IntroPanel extends JPanel{
//-----------------------------------------------------------------
// Sets up this panel with two labels.
//-----------------------------------------------------------------
public IntroPanel(){
setBackground(Color.green);
FES/FSKM/UiTM/JASIN/MELAKA
Flow Layout
FES/FSKM/UiTM/JASIN/MELAKA
Flow Layout
FES/FSKM/UiTM/JASIN/MELAKA
//********************************************************************
// FlowPanel.java Java Foundations
// Represents the panel in the LayoutDemo program that demonstrates
// the flow layout manager.
//********************************************************************
import java.awt.*;
import javax.swing.*;
public class FlowPanel extends JPanel{
//-----------------------------------------------------------------
// Sets up this panel with some buttons to show how flow layout
// affects their position.
//-----------------------------------------------------------------
public FlowPanel(){
setLayout(newFlowLayout());
setBackground(Color.green);
JButton b1 = new JButton("BUTTON 1");
JButton b2 = new JButton("BUTTON 2");
JButton b3 = new JButton("BUTTON 3");
JButton b4 = new JButton("BUTTON 4");
JButton b5 = new JButton("BUTTON 5");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
} FES/FSKM/UiTM/JASIN/MELAKA
}
Border Layout
FES/FSKM/UiTM/JASIN/MELAKA
Border Layout
FES/FSKM/UiTM/JASIN/MELAKA
Border Layout
FES/FSKM/UiTM/JASIN/MELAKA
//********************************************************************
// BorderPanel.java Java Foundations
// Represents the panel in the LayoutDemo program that demonstrates
// the border layout manager.
//********************************************************************
import java.awt.*;
import javax.swing.*;
public class BorderPanel extends JPanel{
//-----------------------------------------------------------------
// Sets up this panel with a button in each area of a border
// layout to show how it affects their position, shape, and size.
//-----------------------------------------------------------------
public BorderPanel(){
setLayout(newBorderLayout());
setBackground(Color.green);
FES/FSKM/UiTM/JASIN/MELAKA
Grid Layout
FES/FSKM/UiTM/JASIN/MELAKA
Grid Layout
FES/FSKM/UiTM/JASIN/MELAKA
//********************************************************************
// GridPanel.java Java Foundations
// Represents the panel in the LayoutDemo program that demonstrates
// the grid layout manager.
//********************************************************************
import java.awt.*;
import javax.swing.*;
public class GridPanel extends JPanel{
//-----------------------------------------------------------------
// Sets up this panel with some buttons to show how grid
// layout affects their position, shape, and size.
//-----------------------------------------------------------------
public GridPanel(){
setLayout(new GridLayout(2, 3));
setBackground(Color.green);
JButton b1 = new JButton("BUTTON 1");
JButton b2 = new JButton("BUTTON 2");
JButton b3 = new JButton("BUTTON 3");
JButton b4 = new JButton("BUTTON 4");
JButton b5 = new JButton("BUTTON 5");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
}
} FES/FSKM/UiTM/JASIN/MELAKA
Box Layout
FES/FSKM/UiTM/JASIN/MELAKA
Box Layout
FES/FSKM/UiTM/JASIN/MELAKA
Box Layout
FES/FSKM/UiTM/JASIN/MELAKA
//********************************************************************
// BoxPanel.java Java Foundations
// Represents the panel in the LayoutDemo program that demonstrates
// the box layout manager.
//********************************************************************
import java.awt.*;
import javax.swing.*;
public class BoxPanel extends JPanel{
//-----------------------------------------------------------------
// Sets up this panel with some buttons to show how a vertical
// box layout (and invisible components) affects their position.
//-----------------------------------------------------------------
public BoxPanel(){
setLayout(newBoxLayout (this, BoxLayout.Y_AXIS));
setBackground(Color.green);
JButton b1 = new JButton("BUTTON 1");
JButton b2 = new JButton("BUTTON 2");
JButton b3 = new JButton("BUTTON 3");
JButton b4 = new JButton("BUTTON 4");
JButton b5 = new JButton("BUTTON 5");
add(b1);
add(Box.createRigidArea(new Dimension (0, 10)));
add(b2);
add(Box.createVerticalGlue());
add(b3);
add(b4);
add(Box.createRigidArea(new Dimension (0, 20)));
add(b5);
} FES/FSKM/UiTM/JASIN/MELAKA
}
Dialog Boxes
FES/FSKM/UiTM/JASIN/MELAKA
EvenOdd Example
FES/FSKM/UiTM/JASIN/MELAKA
//********************************************************************
// EvenOdd.java Java Foundations
//
// Demonstrates the use of the JOptionPane class.
//********************************************************************
import javax.swing.JOptionPane;
do {
numStr = JOptionPane.showInputDialog("Enter an integer: ");
num = Integer.parseInt(numStr);
result = "That number is " + ((num%2 == 0) ? "even" : "odd");
JOptionPane.showMessageDialog(null, result);
again = JOptionPane.showConfirmDialog(null, "Do Another?");
}
while (again == JOptionPane.YES_OPTION);
}
}
FES/FSKM/UiTM/JASIN/MELAKA
JPasswordField
FES/FSKM/UiTM/JASIN/MELAKA
JPasswordField Example
FES/FSKM/UiTM/JASIN/MELAKA
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
public class PasswordFieldDemo extends JFrame {
private JLabel labelPassword;
private JLabel labelConfirmPassword;
private JPasswordField passwordField1;
private JPasswordField passwordField2;
private JButton buttonOK;
FES/FSKM/UiTM/JASIN/MELAKA
public PasswordFieldDemo() {
labelPassword = new JLabel("Enter password:");
labelConfirmPassword = new JLabel("Confirm password:");
passwordField1 = new JPasswordField(20);
passwordField2 = new JPasswordField(20);
buttonOK = new JButton("OK");
add(labelPassword);
add(passwordField1);
add(labelConfirmPassword);
add(passwordField2);
add(buttonOK);
buttonOK.addActionListener (new ButtonListener());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
} FES/FSKM/UiTM/JASIN/MELAKA
private class ButtonListener implements ActionListener{
FES/FSKM/UiTM/JASIN/MELAKA
JCheckBox
FES/FSKM/UiTM/JASIN/MELAKA
JCheckBox Example
FES/FSKM/UiTM/JASIN/MELAKA
import javax.swing.JFrame;
public class StyleOptions{
//-------------------------------------------------------
-
// Creates and displays the style options frame.
//-------------------------------------------------------
-
public static void main (String[] args){
JFrame frame = new JFrame ("Style Options");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new StyleOptionsPanel());
frame.pack();
frame.setVisible(true);
}
}
FES/FSKM/UiTM/JASIN/MELAKA
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StyleOptionsPanel extends JPanel{
private JLabel saying;
private JCheckBox bold, italic;
//-------------------------------------------------------
-
// Sets up a panel with a label and some check boxes
that
// control the style of the label's font.
//-------------------------------------------------------
-
FES/FSKM/UiTM/JASIN/MELAKA
public StyleOptionsPanel(){
saying = new JLabel ("Say it with style!");
saying.setFont (new Font ("Helvetica",
Font.PLAIN,36));
bold = new JCheckBox ("Bold");
bold.setBackground (Color.cyan);
italic = new JCheckBox ("Italic");
italic.setBackground (Color.cyan);
StyleListener listener = new StyleListener();
bold.addItemListener (listener);
italic.addItemListener (listener);
add (saying);
add (bold);
add (italic);
setBackground (Color.cyan);
setPreferredSize (new Dimension(300, 100));
}
FES/FSKM/UiTM/JASIN/MELAKA
//
***********************************************************
// Represents the listener for both check boxes.
//**********************************************************
*
private class StyleListener implements ItemListener{
//----------------------------------------------------
-
// Updates the style of the label font style.
//----------------------------------------------------
-
public void itemStateChanged (ItemEvent event){
int style = Font.PLAIN;
if (bold.isSelected())
style = Font.BOLD;
if (italic.isSelected())
style += Font.ITALIC;
saying.setFont (new Font ("Helvetica", style, 36));
}
FES/FSKM/UiTM/JASIN/MELAKA
}
}
JRadioButton
FES/FSKM/UiTM/JASIN/MELAKA
//
***************************************************************
// QuoteOptions.java Java Foundations
//
// Demonstrates the use of radio buttons.
//
***************************************************************
import javax.swing.JFrame;
frame.pack();
//
***************************************************************
// QuoteOptionsPanel.java Java Foundations
//
// Demonstrates the use of radio buttons.
//
***************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
FES/FSKM/UiTM/JASIN/MELAKA
//--------------------------------------------------------
// Sets up a panel with a label and a set of radio buttons
// that control its text.
//--------------------------------------------------------
public QuoteOptionsPanel(){
comedyQuote = "This is not funny";
philosophyQuote = "I think, therefore I am.";
carpentryQuote = "Measure twice. Cut once.";
FES/FSKM/UiTM/JASIN/MELAKA
ButtonGroup group = new ButtonGroup();
group.add (comedy);
group.add (philosophy);
group.add (carpentry);
add (quote);
add (comedy);
add (philosophy);
add (carpentry);
setBackground (Color.green);
setPreferredSize (new Dimension(300, 100));
}
FES/FSKM/UiTM/JASIN/MELAKA
//*********************************************************
// Represents the listener for all radio buttons
//*********************************************************
private class QuoteListener implements ActionListener{
//------------------------------------------------------
// Sets the text of the label depending on which radio
// button was pressed.
//------------------------------------------------------
public void actionPerformed (ActionEvent event){
Object source = event.getSource();
if (source == comedy)
quote.setText (comedyQuote);
else
if (source == philosophy)
quote.setText (philosophyQuote);
else
quote.setText (carpentryQuote);
}
}
}
FES/FSKM/UiTM/JASIN/MELAKA
References
FES/FSKM/UiTM/JASIN/MELAKA