LAB ACTIVITY 3E
EVENT HANDLING
EVENT HANDLING
Learning Outcomes
By the end of this lab, students should be able to :
Write Java programs using Event Handling
Write Java programs using Event Handling with GUI Components
Hardware/ Software : Personal Computer, Java Development Kit version X.x.x., NetBeans IDE 12.0
LAB ACTIVITY 3E
Activity Outcome: Student know how to create event handling program for checkbox.
CREATE New Project name as TestCheckBox
i. Type the following code:
package testcheckbox;
import java.awt.*;
import javax.swing.*;
class TestCheckBox extends JFrame {
JCheckBox pilihan1 = new JCheckBox();
JCheckBox pilihan2 = new JCheckBox("Regular Pizza");
JCheckBox pilihan3 = new JCheckBox("Small Pizza",false);
String valLarge="";
String valRegular="";
String valSmall="";
String s= " You choose... \n";
TestCheckBox(){
setTitle("Test Checkbox");
setLayout(new FlowLayout());
pilihan1.setText("Large Pizza");
pilihan1.setSelected(false);
pilihan2.setSelected(false);
add (pilihan1);
add (pilihan2);
add (pilihan3);
setSize(400,100);
setVisible(true);
}
public static void main (String[] arg)
{
TestCheckBox pilih =new TestCheckBox();
}
}
ii. Declaration of the event handler class
a) Add package java.awt.event.*;
b) implements ItemListener
example image:
DON'T WORRY about error line under text TesCheckBox. The reasons is not override abstract method valueChanged(ListSelectionEvent) in ListSelectionListener. After when add valueChanged, error line will be disappear.
iii. Registers an instance of the event handler class
pilihan1.addItemListener(this);
pilihan2.addItemListener(this);
pilihan3.addItemListener(this);
example image:
iv. Implements the methods in the listener interface.
public void itemStateChanged(ItemEvent e){
if (e.getSource()==pilihan1)
valLarge=pilihan1.isSelected()? pilihan1.getText() +"\n":"";
if (e.getSource()==pilihan2)
valRegular=pilihan2.isSelected()? pilihan2.getText() +"\n":"";
if (e.getSource()==pilihan3)
valSmall=pilihan3.isSelected()? pilihan3.getText() +"\n":"";
JOptionPane.showMessageDialog(null,s+valLarge+valRegular+valSmall,
"PIZZA",JOptionPane.INFORMATION_MESSAGE);
}
example image:
FULL CODE & EXAMPLE GUI OUTPUT :
package testcheckbox;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class TestCheckBox extends JFrame implements ItemListener{
JCheckBox pilihan1 = new JCheckBox();
JCheckBox pilihan2 = new JCheckBox("Regular Pizza");
JCheckBox pilihan3 = new JCheckBox("Small Pizza",false);
String valLarge="";
String valRegular="";
String valSmall="";
String s= " You choose... \n";
TestCheckBox(){
setTitle("Test Checkbox");
setLayout(new FlowLayout());
pilihan1.setText("Large Pizza");
pilihan1.setSelected(false);
pilihan2.setSelected(false);
pilihan1.addItemListener(this);
pilihan2.addItemListener(this);
pilihan3.addItemListener(this);
add (pilihan1);
add (pilihan2);
add (pilihan3);
setSize(400,100);
setVisible(true);
}
public static void main (String[] arg)
{
TestCheckBox pilih =new TestCheckBox();
}
public void itemStateChanged(ItemEvent e){
if (e.getSource()==pilihan1)
valLarge=pilihan1.isSelected()? pilihan1.getText() +"\n":"";
if (e.getSource()==pilihan2)
valRegular=pilihan2.isSelected()? pilihan2.getText() +"\n":"";
if (e.getSource()==pilihan3)
valSmall=pilihan3.isSelected()? pilihan3.getText() +"\n":"";
JOptionPane.showMessageDialog(null,s+valLarge+valRegular+valSmall,
"PIZZA",JOptionPane.INFORMATION_MESSAGE);
}
}
OUTPUT : (Click on CheckBox to show process)