-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListBox.java
More file actions
71 lines (65 loc) · 1.93 KB
/
ListBox.java
File metadata and controls
71 lines (65 loc) · 1.93 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package AWTAPI;
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame implements ItemListener, ActionListener{
List list;
Choice choice;
TextArea textArea;
MyFrame(){
super("ListBox");
setLayout(new FlowLayout());
list = new List(7,true);
// true means multiple items can be selected
choice = new Choice();
textArea = new TextArea(10,30);
list.add("Sunday");
list.add("Monday");
list.add("Tuesday");
list.add("Wednesday");
list.add("Thursday");
list.add("Friday");
list.add("Saturday");
choice.add("January");
choice.add("February");
choice.add("March");
choice.add("April");
choice.add("May");
choice.add("June");
choice.add("July");
choice.add("August");
choice.add("September");
choice.add("October");
choice.add("November");
choice.add("December");
add(list);
add(choice);
add(textArea);
list.addItemListener(this::itemStateChanged);
choice.addItemListener(this::itemStateChanged);
list.addActionListener(this::actionPerformed);
}
@Override
public void actionPerformed(ActionEvent e) {
String S[] = list.getSelectedItems();
String text ="";
for(String x:S)
text+=x+"\n";
textArea.setText(text);
}
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==list)
textArea.setText(list.getSelectedItem());
else
textArea.setText(choice.getSelectedItem());
}
}
public class EventHandlingAWT {
public static void main(String[] args) {
System.out.println("---------Event Handling in AWT API---------");
MyFrame frame = new MyFrame();
frame.setBackground(Color.LIGHT_GRAY);
frame.setSize(800,500);
frame.setVisible(true);
}
}