-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swing35_1.java
150 lines (117 loc) · 5.26 KB
/
Swing35_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
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
147
148
149
150
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Swing35_1 {
public static void main(String[] args) {
new MyFrame();
}
static class MyFrame extends JFrame{
static Container c;
JTable table;
JButton b1,b2,b3;
JTextField t1,t2,t3;
MyFrame(){
Object[][] data = {{001,"Ali",21},
{002,"Umar",21},
{003,"Abu Bakar",20}};
String[] columnNames = {"Roll Number","Name","Age"};
setBounds(20,20,500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultTableModel model = new DefaultTableModel(data,columnNames);
table = new JTable(model);
setLayout(new GridLayout(3,1));
JPanel panel = new JPanel();
add(new JScrollPane(table));
add(new JPanel());
add(panel);
t1 =new JTextField();
t2 =new JTextField();
t3 =new JTextField();
b1 = new JButton("Add");
b2 = new JButton("Update");
b3 = new JButton("Delete");
panel.setLayout(new GridLayout(3,3));
panel.add(new JLabel("Roll Number"));
panel.add(t1);
panel.add(b1);
panel.add(new JLabel("Names"));
panel.add(t2);
panel.add(b2);
panel.add(new JLabel("Ages"));
panel.add(t3);
panel.add(b3);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(t1.getText().toString().isEmpty() ||
t2.getText().toString().isEmpty() ||
t3.getText().toString().isEmpty() )
JOptionPane.showMessageDialog(null,"Please fill All the Fields",
"Error",JOptionPane.ERROR_MESSAGE);
else {
int rollNumber = Integer.parseInt(t1.getText().toString());
String name = t2.getText().toString();
int age = Integer.parseInt(t3.getText().toString());
Object[] newRow = {rollNumber, name, age};
model.addRow(newRow);
t1.setText(null);
t2.setText(null);
t3.setText(null);
}
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(t1.getText().toString().isEmpty() ||
t2.getText().toString().isEmpty() ||
t3.getText().toString().isEmpty() )
JOptionPane.showMessageDialog(null,"Please fill All the Fields",
"Error",JOptionPane.ERROR_MESSAGE);
else {
int rollNumber = Integer.parseInt(t1.getText().toString());
String name = t2.getText().toString();
int age = Integer.parseInt(t3.getText().toString());
int row = table.getSelectedRow();
model.setValueAt(rollNumber,row,0);
model.setValueAt(name,row,1);
model.setValueAt(age,row,2);
t1.setText(null);
t2.setText(null);
t3.setText(null);
}
}
});
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(table.getSelectedRow() == -1)
JOptionPane.showMessageDialog(null,"Please Select a row",
"Error",JOptionPane.ERROR_MESSAGE);
int selection= JOptionPane.showConfirmDialog(null,
"Do you want to delete a row","Confirm",JOptionPane.YES_NO_OPTION);
if(selection== JOptionPane.YES_OPTION)
model.removeRow(table.getSelectedRow());
}
});
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int rowIndex =table.getSelectedRow();
int rollNumber= (int)model.getValueAt(rowIndex,0);
String name = (String)model.getValueAt(rowIndex,1);
int age = (int)model.getValueAt(rowIndex,2);
t1.setText(String.valueOf(rollNumber));
t2.setText(name);
t3.setText(String.valueOf(age));
}
});
setVisible(true);
}
}
}