CSC444 Java Programming Event Handling

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 67

Chapter 6

CSC444 Java Programming


Event Handling
Graphical User Interface
(GUI)
 Three kinds of object needed to create
 GUI in Java:
 components
 events
 listeners

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

1. instantiate and set up the necessary components,


2. implement listener classes that define what happens
when particular events occur, and
3. establish the relationship between the listeners and
the components that generate the events of interest

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

addActionListener(this);??? Why not 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

 Determines how components in container are arranged


 Consulted when container is resized or new component
are added
 A layout manager determines the size and position of
each component

FES/FSKM/UiTM/JASIN/MELAKA
Layout Managers

 Every layout manager has its own rules and properties


governing the layout of the components it contains
 For some layout managers, the order in which you add
the components affects their positioning
 We use the setLayout method of a container to
change its layout manager

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

 One program, using a tabbed pane, is used to show the


results of various layout managers

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

JTabbedPanetp = new JTabbedPane();


tp.addTab("Intro", new IntroPanel());
tp.addTab("Flow", new FlowPanel());
tp.addTab("Border", new BorderPanel());
tp.addTab("Grid", new GridPanel());
tp.addTab("Box", new BoxPanel());

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

JLabel l1 = new JLabel("Layout Manager Demonstration");


JLabel l2 = new JLabel("Choose a tab to see an example of " +
"a layout manager.");
add(l1);
add(l2);
}
}

FES/FSKM/UiTM/JASIN/MELAKA
Flow Layout

 The JPanel class uses flow layout by default


 Puts as many components as possible on a row, at their
preferred size
 When a component can not fit on a row, it is put on the
next row

FES/FSKM/UiTM/JASIN/MELAKA
Flow Layout

 When the window is resized, the layout manager


automatically repositions the buttons

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

 A border layout has five areas to which components can


be added: North, South, East, West, and Center

FES/FSKM/UiTM/JASIN/MELAKA
Border Layout

 The four outer areas are as large as needed in order to


accommodate the component they contain
 If no components are added to a region, the region
takes up no room in the overall layout
 The Center area expands to fill any available space
 The size of the gaps between the areas can be adjusted

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

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, BorderLayout.CENTER);
add(b2, BorderLayout.NORTH);
add(b3, BorderLayout.SOUTH);
add(b4, BorderLayout.EAST);
add(b5, BorderLayout.WEST);
} FES/FSKM/UiTM/JASIN/MELAKA
}
Grid Layout

 A grid layout presents a container’s components in a


rectangular grid of rows and columns
 One component is placed in each cell, and all cells are
the same size

FES/FSKM/UiTM/JASIN/MELAKA
Grid Layout

 The number of rows and columns in a grid layout is


established by using parameters to the constructor
when the layout manager is created
 As components are added to the grid layout, they fill
the grid from left to right, top to bottom
 There is no way to explicitly assign a component to a
particular location in the grid other than the order in
which they are added to the container

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

 A box layout organizes components either vertically or


horizontally, in one row or one column

FES/FSKM/UiTM/JASIN/MELAKA
Box Layout

 When combined with other layout managers, box layout


can produce complex GUI designs
 Components are organized in the order in which they
are added to the container
 Two types of invisible components can be added to
control spacing between other components
 rigid areas – with a fixed size
 glue – specifies where excess space should go as needed

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

 A dialog box is a graphical window that pops up on top of


any currently active window so that the user can interact
with it
 A dialog box can serve a variety of purposes
 conveying information
 confirming an action
 permitting the user to enter
information
 The JOptionPane class simplifies the creation and
use of basic dialog boxes
FES/FSKM/UiTM/JASIN/MELAKA
Dialog Boxes

 JOptionPane dialog boxes fall into three categories:


 message dialog boxes – used to display an
output string
 input dialog boxes – presents a prompt and a
single input txt file into which the user can
enter one string of data
 confirm dialog box – presents the user with a
simple yes-or-no question
 These three types of dialog boxes are created using static methods
in the JOptionPane class.
 Many of the JOptionPane methods allow the program to
tailor the contents of the dialog box.
FES/FSKM/UiTM/JASIN/MELAKA
JOptionPane Methods

FES/FSKM/UiTM/JASIN/MELAKA
EvenOdd Example

 Determines if an integer is even or odd


 Instead of a single frame, it uses three dialog boxes

FES/FSKM/UiTM/JASIN/MELAKA
//********************************************************************
// EvenOdd.java Java Foundations
//
// Demonstrates the use of the JOptionPane class.
//********************************************************************

import javax.swing.JOptionPane;

public class EvenOdd{


//-----------------------------------------------------------------
// Determines if the value input by the user is even or odd.
// Uses multiple dialog boxes for user interaction.
//-----------------------------------------------------------------
public static void main(String[] args){
String numStr, result;
int num, again;

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

 Shows that character was typed as user enters


characters, but hides the characters assuming that they
represent a password that should remain known only to
users.

FES/FSKM/UiTM/JASIN/MELAKA
JPasswordField Example

 User enter the passwords, if both passwords are


matched a dialog box will display “Congratulations!
You entered correct password”, if the passwords do
not match, the dialog box will display “Passwords do
not match!”, else the dialog box will display “Wrong
password!”.

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

You have to set layout first, then only can


Add component into the layout…
setLayout(new GridLayout(3,2));

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{

public void actionPerformed (ActionEvent event){


char[] password1 = passwordField1.getPassword();
char[] password2 = passwordField2.getPassword();
 
if (!Arrays.equals(password1, password2)) {
JOptionPane.showMessageDialog(PasswordFieldDemo.this,
“Passwords do not match!");
return;
}
 
char[] correctPass = new char[] {'c', 's', 'c', '5', '4',
'1'};
if (Arrays.equals(password1, correctPass)) {
JOptionPane.showMessageDialog(PasswordFieldDemo.this,
"Congratulations! You entered correct password.");
}
else {
JOptionPane.showMessageDialog(PasswordFieldDemo.this,
"Wrong password!");
}
FES/FSKM/UiTM/JASIN/MELAKA
}
}
public static void main(String[] args){
new PasswordFieldDemo().setVisible(true);
}
}

FES/FSKM/UiTM/JASIN/MELAKA
JCheckBox

 Button that can be toggled on or off using the mouse,


indicating that particular boolean condition is set or
unset.
 Each checkbox operate independently, i.e. each can be
set to on or off and the status does not influence the
others.
 A check box generates an item event when it changes
state from selected (checked) to deselected
(unchecked) and vice versa

FES/FSKM/UiTM/JASIN/MELAKA
JCheckBox Example

 The phrase is displayed in regular font style, bold,


italic, or both, depending on which check boxes are
selected

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

A radio button is used with other radio buttons


to provide a set of mutually exclusive options
 Radio buttons have meaning only when used
with one or more other radio buttons
 At any point in time, only one button of the
group is selected (on)
 Radio buttons produce an action event when
selected
 The ButtonGroup class is used to define a set
of related radio buttons
FES/FSKM/UiTM/JASIN/MELAKA
QuoteOptions Example

 A different quote is displayed depending on which radio


button is selected
 Since only one quote is displayed at any time, mutually-
exclusive radio buttons are used

FES/FSKM/UiTM/JASIN/MELAKA
//
***************************************************************
// QuoteOptions.java Java Foundations
//
// Demonstrates the use of radio buttons.
//
***************************************************************

import javax.swing.JFrame;

public class QuoteOptions{


//-----------------------------------------------------------
-
// Creates and presents the program frame.
//-----------------------------------------------------------
-
public static void main (String[] args){
JFrame frame = new JFrame ("Quote Options");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add (new QuoteOptionsPanel());


FES/FSKM/UiTM/JASIN/MELAKA

frame.pack();
//
***************************************************************
// QuoteOptionsPanel.java Java Foundations
//
// Demonstrates the use of radio buttons.
//
***************************************************************

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class QuoteOptionsPanel extends JPanel


{
private JLabel quote;
private JRadioButton comedy, philosophy, carpentry;
private String comedyQuote, philosophyQuote,
carpentryQuote;

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.";

quote = new JLabel (comedyQuote);


quote.setFont (new Font ("Helvetica", Font.BOLD, 24));

comedy = new JRadioButton ("Comedy", true);


comedy.setBackground (Color.green);
philosophy = new JRadioButton ("Philosophy");
philosophy.setBackground (Color.green);
carpentry = new JRadioButton ("Carpentry");
carpentry.setBackground (Color.green);

FES/FSKM/UiTM/JASIN/MELAKA
ButtonGroup group = new ButtonGroup();
group.add (comedy);
group.add (philosophy);
group.add (carpentry);

QuoteListener listener = new QuoteListener();


comedy.addActionListener (listener);
philosophy.addActionListener (listener);
carpentry.addActionListener (listener);

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

 Java Foundations, 3rd Edition,Lewis/DePasquale/


Chase
 http://docs.oracle.com/javase/tutorial/uiswing/
components/index.html

FES/FSKM/UiTM/JASIN/MELAKA

You might also like