-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swing51_1.java
88 lines (78 loc) · 2.76 KB
/
Swing51_1.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Swing51_1 extends JFrame {
public static void main(String[] args) {
new Swing51_1();
}
int updateIndex = 0;
Swing51_1() {
setLayout(new GridLayout(1, 2));
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
panel = new JPanel();
panel2 = new JPanel();
list = new JList();
textField = new JTextField(15);
button1 = new JButton("Add");
button2 = new JButton("Update");
button3 = new JButton("Delete");
DefaultListModel model = new DefaultListModel();
model.addElement("10th");
model.addElement("12th");
model.addElement("Graduation");
model.addElement("Post-Graduation");
list.setModel(model);
add(panel);
add(panel2);
panel.add(list);
panel.setBackground(Color.BLACK);
panel2.add(textField);
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!textField.getText().toString().isEmpty()) {
String input = textField.getText().toString();
model.addElement(input);
textField.setText(null);
}
}
});
list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
updateIndex = list.getSelectedIndex();
textField.setText((String) model.get(updateIndex));
}
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!textField.getText().toString().isEmpty()) {
String input = textField.getText().toString();
model.set(updateIndex, input);
textField.setText(null);
}
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!textField.getText().toString().isEmpty())
model.remove(updateIndex);
}
});
revalidate();
}
JPanel panel , panel2;
JList list;
JTextField textField;
JButton button1 , button2 , button3;
}