-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd ItemListener to JCheckbox
51 lines (49 loc) · 1.33 KB
/
Add ItemListener to JCheckbox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.hubberspot.awtSwing.example;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CheckBoxItemListener implements ItemListener
{
JFrame frame;
JCheckBox checkBoxArray[] = new JCheckBox[3];
JTextField textField;
JLabel label;
JPanel panel;
CheckBoxItemListener()
{
frame = new JFrame("JCheckBox Event Handling");
frame.setLayout(new GridLayout(3,1));
panel = new JPanel();
checkBoxArray[0] = new JCheckBox("Java");
checkBoxArray[1] = new JCheckBox("C++");
checkBoxArray[2] = new JCheckBox("C");
for(int i = 0; i<3; i++)
{
panel.add(checkBoxArray[i]);
checkBoxArray[i].addItemListener(this);
}
textField = new JTextField(20);
label = new JLabel("Select the Programming " +
"Language you have used : ", JLabel.CENTER);
frame.add(label);
frame.add(panel);
frame.add(textField);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent ie)
{
String message="You have used: ";
for(int i=0; i<3; i++)
{
if(checkBoxArray[i].isSelected())
message += checkBoxArray[i].getText() + " ";
}
textField.setText(message);
}
public static void main(String[] args)
{
new CheckBoxItemListener();
}
}