LAB ACTIVITY 3D
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 3D
Activity Outcome: Student know how to create event handling program for list.
CREATE New Project name as TestList
i. Type the following code:
package testcombobox;
import java.awt.*;
import javax.swing.*;
class TestList extends JFrame{
JTextField entry = new JTextField (15);
JLabel lblMonth = new JLabel("You choose :");
String blnHijrah[] =
{"Muharam","Safar","Rabiulawal","Rabiulakhir","Jamadilawal",
"Jamadilakhir","Rejab","Syaaban","Ramadhan","Syawal","Zulkaedah","Zulhijjah"};
JList list = new JList(blnHijrah);
TestList()
{
setTitle("Test List");
setLayout(new FlowLayout());
list.setFont(new Font("Tahoma",Font.BOLD+Font.ITALIC,14));
list.setVisibleRowCount(5);
list.setLayoutOrientation(JList.VERTICAL);
//list.setLayoutOrientation(JList.VERTICAL_WRAP);
//list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
add(list);
add(lblMonth);
add(entry);
setSize(300,400);
setVisible(true);
}
public static void main (String[] arg)
{
TestList senarai =new TestList();
}
}
ii. Declaration of the event handler class
a) Add package java.swing.event.*;
b) implements ListSelectionListener
example image:
DON'T WORRY about error line under text TestList. 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
list.addListSelectionListener(this);
example image:
iv. Implements the methods in the listener interface.
public void valueChanged(ListSelectionEvent e)
{
String Month = list.getSelectedValue().toString();
entry.setText(Month);
}
example image:
FULL CODE & EXAMPLE GUI OUTPUT :
package testcombobox;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
class TestList extends JFrame implements ListSelectionListener{
JTextField entry = new JTextField (15);
JLabel lblMonth = new JLabel("You choose :");
String blnHijrah[] =
{"Muharam","Safar","Rabiulawal","Rabiulakhir","Jamadilawal",
"Jamadilakhir","Rejab","Syaaban","Ramadhan","Syawal","Zulkaedah","Zulhijjah"};
JList list = new JList(blnHijrah);
TestList()
{
setTitle("Test List");
setLayout(new FlowLayout());
list.setFont(new Font("Tahoma",Font.BOLD+Font.ITALIC,14));
list.setVisibleRowCount(5);
list.setLayoutOrientation(JList.VERTICAL);
//list.setLayoutOrientation(JList.VERTICAL_WRAP);
//list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.addListSelectionListener(this);
add(list);
add(lblMonth);
add(entry);
setSize(300,400);
setVisible(true);
}
public static void main (String[] arg)
{
TestList senarai =new TestList();
}
public void valueChanged(ListSelectionEvent e)
{
String Month = list.getSelectedValue().toString();
entry.setText(Month);
}
}
OUTPUT : (Select list at List to show text at TextField)