-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable
More file actions
280 lines (221 loc) · 8.19 KB
/
Table
File metadata and controls
280 lines (221 loc) · 8.19 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package taoshiflex.internetmanagementsystem;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Table extends JFrame implements ActionListener {
private Container c;
private Font font;
private JLabel label, label1, id, name, ip, net, price;
private JTextField idtf, nametf, iptf, nettf, pricetf;
private JButton add, update, delete, refresh, bk;
private JTable table;
private DefaultTableModel model;
private JScrollPane scroll;
JFrame f;
private String[] col = {"ID", "Name", "Number", "Ethernet", "Price"};
private String[] row = new String[5];
Table() {
initComponets();
}
private void initComponets() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(700, 800);
this.setLocationRelativeTo(null);
this.setTitle("Table");
c = this.getContentPane();
c.setLayout(null);
c.setBackground(Color.PINK);
font = new Font("Trebuchet MS", Font.BOLD, 16);
// Button & TextField
label = new JLabel("Internet Bandwidth Management System");
label.setBounds(200, 20, 800, 50);
label.setFont(font);
c.add(label);
label1 = new JLabel("TMSnetBD");
label1.setBackground(new java.awt.Color(255, 255, 255));
label1.setFont(new java.awt.Font("Trebuchet MS", 1, 50)); // NOI18N
label1.setBounds(200, 700, 600, 60);
add(label1);
id = new JLabel("ID");
id.setBounds(20, 80, 100, 30);
id.setFont(font);
c.add(id);
idtf = new JTextField();
idtf.setBounds(180, 80, 150, 30);
idtf.setFont(font);
c.add(idtf);
name = new JLabel("Name");
name.setBounds(20, 120, 100, 30);
name.setFont(font);
c.add(name);
nametf = new JTextField();
nametf.setBounds(180, 120, 150, 30);
nametf.setFont(font);
c.add(nametf);
ip = new JLabel("IP Address");
ip.setBounds(20, 160, 100, 30);
ip.setFont(font);
c.add(ip);
iptf = new JTextField();
iptf.setBounds(180, 160, 150, 30);
iptf.setFont(font);
c.add(iptf);
net = new JLabel("Ethernet");
net.setBounds(20, 200, 100, 30);
net.setFont(font);
c.add(net);
nettf = new JTextField();
nettf.setBounds(180, 200, 150, 30);
nettf.setFont(font);
c.add(nettf);
price = new JLabel("Price");
price.setBounds(20, 240, 100, 30);
price.setFont(font);
c.add(price);
pricetf = new JTextField();
pricetf.setBounds(180, 240, 150, 30);
pricetf.setFont(font);
c.add(pricetf);
add = new JButton("ADD");
add.setBounds(100, 300, 120, 30);
add.setFont(font);
c.add(add);
update = new JButton("Update");
update.setBounds(250, 300, 120, 30);
update.setFont(font);
c.add(update);
delete = new JButton("Delete");
delete.setBounds(400, 300, 120, 30);
delete.setFont(font);
c.add(delete);
refresh = new JButton("Refresh");
refresh.setBounds(400, 80, 120, 30);
refresh.setFont(font);
c.add(refresh);
bk = new JButton("Back");
bk.setBackground(new java.awt.Color(255, 255, 255));
bk.setFont(new java.awt.Font("Trebuchet MS", 1, 25));
bk.setBounds(260, 660, 100, 30);
bk.addActionListener(this);
add(bk);
// Table
table = new JTable();
model = new DefaultTableModel();
model.setColumnIdentifiers(col);
table.setModel(model);
table.setFont(font);
table.setSelectionBackground(Color.yellow);
table.setBackground(Color.WHITE);
table.setRowHeight(30);
scroll = new JScrollPane(table);
scroll.setBounds(10, 360, 665, 260);
c.add(scroll);
// Action call
add.addActionListener(this);
update.addActionListener(this);
delete.addActionListener(this);
refresh.addActionListener(this);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
int rowNumber = table.getSelectedRow();
String me_id = model.getValueAt(rowNumber, 0).toString();
String me_name = model.getValueAt(rowNumber, 1).toString();
String me_ip = model.getValueAt(rowNumber, 2).toString();
String me_net = model.getValueAt(rowNumber, 3).toString();
String me_price = model.getValueAt(rowNumber, 4).toString();
idtf.setText(me_id);
nametf.setText(me_name);
iptf.setText(me_ip);
nettf.setText(me_net);
pricetf.setText(me_price);
}
});
}
void clearall() {
idtf.setText("");
nametf.setText("");
iptf.setText("");
nettf.setText("");
pricetf.setText("");
}
@Override
public void actionPerformed(ActionEvent a) {
//add
if (a.getSource() == add) {
row[0] = idtf.getText();
row[1] = nametf.getText();
row[2] = iptf.getText();
row[3] = nettf.getText();
row[4] = pricetf.getText();
model.addRow(row);
JOptionPane.showMessageDialog(null, "user successfully added");
clearall();
} //delete
else if (a.getSource() == delete) {
int rowNumber = table.getSelectedRow();
if (rowNumber >= 0) {
int show = JOptionPane.showConfirmDialog(null, "are you sure you want to remove \n" + nametf.getText());
if (show == JOptionPane.YES_OPTION) {
model.removeRow(rowNumber);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
clearall();
} else {
JOptionPane.showMessageDialog(null, "no Row has been seleted or no Row has been exits");
}
} //update
else if (a.getSource() == update) {
int rowNumber = table.getSelectedRow();
if (rowNumber >= 0) {
int show = JOptionPane.showConfirmDialog(null, "are you sure you want to update \n" + nametf.getText());
if (show == JOptionPane.YES_OPTION) {
String m_id = idtf.getText();
String m_name = nametf.getText();
String m_ip = iptf.getText();
String m_net = nettf.getText();
String m_price = pricetf.getText();
model.setValueAt(m_id, rowNumber, 0);
model.setValueAt(m_name, rowNumber, 1);
model.setValueAt(m_ip, rowNumber, 2);
model.setValueAt(m_net, rowNumber, 3);
model.setValueAt(m_price, rowNumber, 4);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
clearall();
}
} else if (a.getSource() == refresh) {
} else if (a.getSource() == bk) {
try {
setVisible(false);
new LoginPage();
} catch (Exception ex) {
System.out.println(ex);
}
}
try {
File file = new File("data.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < model.getRowCount(); i++) {
for (int j = 0; j < model.getColumnCount(); j++) {
bw.write((String) model.getValueAt(i, j) + " ");
}
}
bw.close();
fw.close();
} catch (IOException x) {
System.out.println(x);
}
}
}