-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItemAndTextListener.java
146 lines (133 loc) · 4.05 KB
/
ItemAndTextListener.java
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package AWTAPI;
import java.awt.*;
import java.awt.event.*;
/*
class MyFrame extends Frame implements ActionListener, ItemListener {
Label label;
Label counter;
Button button;
Checkbox c1,c2,c3,c4;
CheckboxGroup radio;
int count=0;
MyFrame(){
super(" Event Handling");
setLayout(new FlowLayout());
label = new Label("Nothing is selected\n");
counter = new Label(" "+count);
button = new Button("Click");
button.addActionListener(this::actionPerformed);
radio = new CheckboxGroup();
// c1 = new Checkbox("Machine Learning");
// c2 = new Checkbox("AWS");
// c3 = new Checkbox("Data Structure");
// c4 = new Checkbox("Competitive Programming");
c1 = new Checkbox("Machine Learning",false,radio);
c2 = new Checkbox("AWS",false,radio);
c3 = new Checkbox("Data Structure",false,radio);
c4 = new Checkbox("Competitive Programming",false,radio);
c1.addItemListener(this::itemStateChanged);
c2.addItemListener(this::itemStateChanged);
c3.addItemListener(this::itemStateChanged);
c4.addItemListener(this::itemStateChanged);
add(label);
add(counter);
add(c1);
add(c2);
add(c3);
add(c4);
add(button);
}
@Override
public void actionPerformed(ActionEvent e) {
counter.setText(" "+(++count));
}
@Override
public void itemStateChanged(ItemEvent e) {
String option="";
if(c1.getState())
option = option+" "+c1.getLabel();
if(c2.getState())
option = option+" "+c2.getLabel();
if(c3.getState())
option = option+" "+c3.getLabel();
if(c4.getState())
option = option+" "+c4.getLabel();
if(option.isEmpty())
option = "Nothing is selected";
label.setText(option+" ");
}
}
*/
/*
class MyFrame extends Frame implements TextListener, ActionListener{
Label l1, l2, p;
TextField TF, PF;
MyFrame(){
super("TextField");
l1 = new Label("UserName");
p = new Label("Password");
l2 = new Label("Hit Enter for Submit");
TF = new TextField(20);
PF = new TextField(10);
PF.setEchoChar('*');
setLayout(new FlowLayout());
TF.addTextListener(this::textValueChanged);
TF.addActionListener(this::actionPerformed);
PF.addActionListener(this::actionPerformed);
add(l1);
add(l2);
add(TF);
add(PF);
add(p);
}
@Override
public void actionPerformed(ActionEvent e) {
l2.setText("Submitted");
}
@Override
public void textValueChanged(TextEvent e) {
l1.setText(TF.getText());
}
}
*/
class MyFrame extends Frame implements TextListener, ActionListener{
Label l1, l2;
TextArea textArea;
TextField textField;
Button button;
MyFrame(){
super("TextArea");
l1 = new Label("TextArea");
l2 = new Label("TextField");
textArea = new TextArea(10,20);
textField = new TextField(20);
button = new Button("Submit");
setLayout(new FlowLayout());
add(l1);
add(l2);
add(textArea);
add(textField);
add(button);
textArea.addTextListener(this::textValueChanged);
textField.addTextListener(this::textValueChanged);
button.addActionListener(this::actionPerformed);
}
@Override
public void actionPerformed(ActionEvent e) {
// l1.setText(textArea.getSelectedText());
// textArea.append(textField.getText());
textArea.insert(textField.getText(),textArea.getCaretPosition());
}
@Override
public void textValueChanged(TextEvent e) {
}
}
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);
}
}