LAB ACTIVITY 3F
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 3F
Activity Outcome: Student know how to create event handling program for radio button.
CREATE New Project name as TestRadioButton
i. Type the following code:
package testradiobutton;
import java.awt.*;
import javax.swing.*;
class TestRadioButton extends JFrame {
private ButtonGroup pilihan = new ButtonGroup();
private JRadioButton pilihan1 = new JRadioButton();
private JRadioButton pilihan2 = new JRadioButton("Medium Pizza");
private JRadioButton pilihan3 = new JRadioButton("Small Pizza",false);
TestRadioButton(){
setTitle("Test RadioButton");
setLayout(new FlowLayout());
pilihan1.setText("Large Pizza");
pilihan1.setSelected(false);
pilihan2.setSelected(false);
pilihan.add (pilihan1);
pilihan.add (pilihan2);
pilihan.add (pilihan3);
add (pilihan1);
add (pilihan2);
add (pilihan3);
setSize(400,100);
setVisible(true);
}
public static void main (String[] arg)
{
TestRadioButton pilih =new TestRadioButton();
}
}
ii. Declaration of the event handler class
a) Add package java.awt.event.*;
b) implements ItemListener
example image:
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 (pilihan1.isSelected())
JOptionPane.showMessageDialog(null,"YOU HAVE CHOOSE LARGE PIZZA"
,"PIZZA",JOptionPane.INFORMATION_MESSAGE);
else if (pilihan2.isSelected())
JOptionPane.showMessageDialog(null,"YOU HAVE CHOOSE MEDIUM PIZZA"
,"PIZZA",JOptionPane.INFORMATION_MESSAGE);
else if (pilihan3.isSelected())
JOptionPane.showMessageDialog(null,"YOU HAVE CHOOSE SMALL PIZZA",
"PIZZA",JOptionPane.INFORMATION_MESSAGE);
}
example image:
FULL CODE & EXAMPLE GUI OUTPUT :
package testradiobutton;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class TestRadioButton extends JFrame implements ItemListener{
private ButtonGroup pilihan = new ButtonGroup();
private JRadioButton pilihan1 = new JRadioButton();
private JRadioButton pilihan2 = new JRadioButton("Medium Pizza");
private JRadioButton pilihan3 = new JRadioButton("Small Pizza",false);
TestRadioButton(){
setTitle("Test RadioButton");
setLayout(new FlowLayout());
pilihan1.setText("Large Pizza");
pilihan1.setSelected(false);
pilihan2.setSelected(false);
pilihan.add (pilihan1);
pilihan.add (pilihan2);
pilihan.add (pilihan3);
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)
{
TestRadioButton pilih =new TestRadioButton();
}
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 RadioButton to show process)