-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminGUI.java
More file actions
611 lines (576 loc) · 29.6 KB
/
AdminGUI.java
File metadata and controls
611 lines (576 loc) · 29.6 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package PAT;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
/*
adminGUI class
Front end of the Admin class and provides user with interface to interact with Users table in hostel database
*/
public class AdminGUI extends javax.swing.JFrame {
String userWelcome = ""; //Welcome message displayed in Main GUI
String userType = ""; // The current user using the program's type
Admin adm = new Admin(); // An instance of the Admin class
ResultSet rs; // Resultset variable to store data retrieved form admin class
String foundID = ""; //contains the ID of the user found in the search GUI
/*
Constructor Method #1
Used when user is found in the search GUI and the found user is preloaded into the data fields
@parameters: String cureent user's type, String User welcome message, String ID of the person found
@return: none
*/
public AdminGUI(String _userType, String _userWelcome, String _foundID){
foundID = _foundID;
userType = _userType;
userWelcome = _userWelcome;
initComponents();
updateTable(); //loads database info into the table
insertFoundID(foundID); //preloads found user's data
}
/*
Constructor Method #2
Used when a new instance of the AdminGUI is created without a user needed to be preloaded
@parameters: String userType, String user welcome message
@return: none
*/
public AdminGUI(String _userType, String _userWelcome){
userType = _userType;
userWelcome = _userWelcome;
initComponents();
updateTable();//loads database info into the table
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
btnBack = new javax.swing.JButton();
btnClear = new javax.swing.JButton();
lblUserPassword = new javax.swing.JLabel();
btnUpdate = new javax.swing.JButton();
lblSurname = new javax.swing.JLabel();
btnInsert = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
btnSearch = new javax.swing.JButton();
lblUserType = new javax.swing.JLabel();
lblUserActive = new javax.swing.JLabel();
txtFieldUsername = new javax.swing.JTextField();
txtFieldPassword = new javax.swing.JTextField();
lblADminGUI = new javax.swing.JLabel();
btnAdmHelp = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tblUsers = new javax.swing.JTable();
lblUsername = new javax.swing.JLabel();
txtFieldSurname = new javax.swing.JTextField();
cmbUserType = new javax.swing.JComboBox<>();
lblName = new javax.swing.JLabel();
txtFieldName = new javax.swing.JTextField();
cboxUserActive = new javax.swing.JCheckBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnBack.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
btnBack.setText("BACK");
btnBack.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
btnBack.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBackActionPerformed(evt);
}
});
btnClear.setText("Clear");
btnClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
lblUserPassword.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblUserPassword.setText("Password:");
btnUpdate.setText("Update");
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateActionPerformed(evt);
}
});
lblSurname.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblSurname.setText("Surname:");
btnInsert.setText("Insert");
btnInsert.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnInsertActionPerformed(evt);
}
});
btnDelete.setText("Remove");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});
btnSearch.setText("Search");
btnSearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSearchActionPerformed(evt);
}
});
lblUserType.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblUserType.setText("Type:");
lblUserActive.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblUserActive.setText("Active:");
txtFieldUsername.setEditable(false);
txtFieldUsername.setBackground(new java.awt.Color(255, 255, 255));
lblADminGUI.setBackground(new java.awt.Color(255, 255, 255));
lblADminGUI.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
lblADminGUI.setText(" ADMIN");
lblADminGUI.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
btnAdmHelp.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
btnAdmHelp.setText("Help");
btnAdmHelp.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
btnAdmHelp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAdmHelpActionPerformed(evt);
}
});
tblUsers.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null}
},
new String [] {
"Username", "Type", "Password", "Name", "Surname", "Active"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
tblUsers.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tblUsersMouseClicked(evt);
}
});
jScrollPane1.setViewportView(tblUsers);
lblUsername.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblUsername.setText("Username:");
cmbUserType.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Admin", "Coaching", "Kitchen" }));
lblName.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblName.setText("Name:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(72, 72, 72)
.addComponent(lblADminGUI, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(80, 80, 80)
.addComponent(btnAdmHelp, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 659, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(152, 152, 152)
.addComponent(lblUserType, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(240, 240, 240)
.addComponent(cmbUserType, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(152, 152, 152)
.addComponent(lblName, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(240, 240, 240)
.addComponent(txtFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(152, 152, 152)
.addComponent(lblSurname, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(240, 240, 240)
.addComponent(txtFieldSurname, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(152, 152, 152)
.addComponent(lblUserActive, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(278, 278, 278)
.addComponent(cboxUserActive))
.addGroup(layout.createSequentialGroup()
.addGap(344, 344, 344)
.addComponent(btnClear))
.addGroup(layout.createSequentialGroup()
.addGap(152, 152, 152)
.addComponent(btnUpdate)
.addGap(50, 50, 50)
.addComponent(btnInsert)
.addGap(57, 57, 57)
.addComponent(btnDelete)
.addGap(28, 28, 28)
.addComponent(btnSearch))
.addGroup(layout.createSequentialGroup()
.addGap(152, 152, 152)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(lblUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblUserPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(223, 223, 223)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtFieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtFieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(24, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblADminGUI, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnAdmHelp, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblUsername)
.addGroup(layout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(txtFieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblUserType)
.addGroup(layout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(cmbUserType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblUserPassword)
.addGroup(layout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(txtFieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblName)
.addGroup(layout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(txtFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblSurname)
.addGroup(layout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(txtFieldSurname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblUserActive)
.addComponent(cboxUserActive))
.addGap(4, 4, 4)
.addComponent(btnClear)
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnUpdate)
.addComponent(btnInsert)
.addComponent(btnDelete)
.addComponent(btnSearch)))
);
pack();
}// </editor-fold>//GEN-END:initComponents
/*
Method of the Back Button
Opens an instance of the MainGUI class and then closes the AdminGUI class
*/
private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
dispose();
MainGUI mai = new MainGUI(userType, userWelcome);
mai.setVisible(true); //set main GUI to visible
}//GEN-LAST:event_btnBackActionPerformed
/*
Method of the clear button
Clears all the data fields
*/
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
clearFields(); //Method that clears all the data fields
}//GEN-LAST:event_btnClearActionPerformed
/*
Method of the Update button
Retrieves all the data from the data fields and uses it to update current user
*/
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
if(txtFieldUsername.getText().isEmpty() == false ){ //checks to see that user has been selected
//Checks if the name, surname and password field have been inserted
if(txtFieldName.getText().isEmpty() == false && txtFieldSurname.getText().isEmpty() == false && txtFieldPassword.getText().isEmpty() == false){
//Retrieves all the info from the data fields
String username = txtFieldUsername.getText();
String type = (String)cmbUserType.getSelectedItem();
String password = txtFieldPassword.getText();
String name = txtFieldName.getText();
String surname = txtFieldSurname.getText();
boolean active = cboxUserActive.isSelected();
//Method that updates the selected user
adm.updateUser(username, type, password, name, surname, active);
//Updates the table with the new info
updateTable();
//Clear all the data fields
clearFields();
//Notify user that the person has been updated
JOptionPane.showMessageDialog(null, "User has been updated");
}else{
//Advise user to fill in the missing data
JOptionPane.showMessageDialog(null, "Name and/or Surname and/or Password fields are empty, please fill them.");
}
}else{
//Advise user to select a user.
JOptionPane.showMessageDialog(null, "No user has been selected");
}
}//GEN-LAST:event_btnUpdateActionPerformed
/*
Method of the Insert button
Retrieves all the information from the data fields to compile a new user and insert it into the database
*/
private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertActionPerformed
if(txtFieldUsername.getText().isEmpty() == true){//Checks that no user is selected
//Checks if the name, surname and password field have been inserted
if(txtFieldName.getText().isEmpty() == false && txtFieldSurname.getText().isEmpty() == false && txtFieldPassword.getText().isEmpty() == false){
//Retrieves all the info from the data fields
String type = (String)cmbUserType.getSelectedItem();
String password = txtFieldPassword.getText();
String name = txtFieldName.getText();
String surname = txtFieldSurname.getText();
boolean active = cboxUserActive.isSelected();
//Inserts the new user with the admin method insertUser
adm.insertUser(type, password, name, surname, active); //
//Update the table with the new information
updateTable();
//Clear all the data fields
clearFields();
//Notify the user that the new user has been inserted
JOptionPane.showMessageDialog(null, "User has been entered into the database.");
}else{
//Advise the user to enter the missing information
JOptionPane.showMessageDialog(null, "Name and/or Surname and/or Password fields are empty, please fill them.");
}
}else{
//Advise user to clear fields before continuing and accidentally inserting an existing user
JOptionPane.showMessageDialog(null, "User currently selected. Clear fields first and then enter data.");
}
}//GEN-LAST:event_btnInsertActionPerformed
/*
Method of the delete button
Uses the given username to lacte and delete the desired user in the database
*/
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
//Checks that a user has been selected
if(txtFieldUsername.getText().isEmpty() == false){
//gets the username from the data field
String username = txtFieldUsername.getText();
//use delete method from Admin class
adm.deleteUser(username);
// Update table with new information
updateTable();
//Clear all the data fields
clearFields();
//Notify User that the desired user has been deleted
JOptionPane.showMessageDialog(null, "User has been removed from the database.");
}else{
//Advise user to select a user before attempting delete
JOptionPane.showMessageDialog(null, "No User is selected. Select or Search for User");
}
}//GEN-LAST:event_btnDeleteActionPerformed
/*
Method for search button
Takes user to the AdminSearchGUI to find the desired user
*/
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
AdminSearchGUI search = new AdminSearchGUI(userType, userWelcome);
search.setVisible(true);
dispose();
}//GEN-LAST:event_btnSearchActionPerformed
/*
Method of the Help Button
Opens Help Text
*/
private void btnAdmHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdmHelpActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_btnAdmHelpActionPerformed
/*
Table Click Method
When user clicks on a record on the table this method loads the records info into the data
fields.
*/
private void tblUsersMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblUsersMouseClicked
int row = tblUsers.rowAtPoint(evt.getPoint()); //get the row number of the selected record
populateFields(row); //use method to populate the data fields
}//GEN-LAST:event_tblUsersMouseClicked
/*
Method updateTable
Used to insert all the new data into the table
@parameters: none
@return: none
*/
private void updateTable(){
clearTable(); //Method that clears all the current table data
try {
rs = adm.getUserInfo(); //method that retrieves all user information
int count = 0;
while(rs.next()){
//extract all the information of every user
String username = rs.getString("username");
String type = rs.getString("userType");
String password = rs.getString("uPassword");
String name = rs.getString("uName");
String surname = rs.getString("uSurname");
boolean active = rs.getBoolean("active");
String data[] = {username, type, password, name, surname};
DefaultTableModel model = (DefaultTableModel)tblUsers.getModel();
//Insert information into table
model.addRow(data);
//Insert Boolean data type into the table which is not a string so it has
// to be done seperately
model.setValueAt(active, count, 5);
count ++;
}
} catch (SQLException ex) {
Logger.getLogger(AdminGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
/*
Method clearTable
Method that removes all the current information from the table in the GUI
@parameters: none
@return: none
*/
private void clearTable(){
//Get table model so that it can be manipulated
DefaultTableModel model = (DefaultTableModel)tblUsers.getModel();
//Remove each row
while(model.getRowCount()> 0){
model.removeRow(0);
}
}
/*
Method populateFields
Loads all the user's info into the data fields.
@parameters: int row in the table that has been selected
@return: none
*/
private void populateFields(int row){
//Gets data from the table and inserts it into the data fields
txtFieldUsername.setText((String)tblUsers.getValueAt(row, 0));
String Type = (String)tblUsers.getValueAt(row, 1);
cmbUserType.setSelectedItem(Type);
txtFieldPassword.setText((String)tblUsers.getValueAt(row, 2));
txtFieldName.setText((String)tblUsers.getValueAt(row, 3));
txtFieldSurname.setText((String)tblUsers.getValueAt(row, 4));
cboxUserActive.setSelected((boolean)tblUsers.getValueAt(row, 5));
}
/*
Method clearFields
Clears all the data fields
@parameters: none
@return: none
*/
private void clearFields(){
//Clears every data field individually
txtFieldUsername.setText("");
String Type = "Admin";
cmbUserType.setSelectedItem(Type);
txtFieldPassword.setText("");
txtFieldName.setText("");
txtFieldSurname.setText("");
cboxUserActive.setSelected(false);
}
/*
Method insertFoundID
Inserts all the info of the found user into the data fields
@parameters: the ID of the user found in the adminSearchGUI
@return: none
*/
private void insertFoundID(String _foundID){
int row = 0;
boolean rowFound = false;
//Checks to see which row in the table the user is located at
while(rowFound == false){
if(_foundID.equalsIgnoreCase((String)tblUsers.getValueAt(row, 0))){
rowFound = true;
}else{
row++;
}
}
//Insert the row's information into the data fields
populateFields(row);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(AdminGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AdminGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AdminGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AdminGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AdminGUI("","").setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnAdmHelp;
private javax.swing.JButton btnBack;
private javax.swing.JButton btnClear;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnInsert;
private javax.swing.JButton btnSearch;
private javax.swing.JButton btnUpdate;
private javax.swing.JCheckBox cboxUserActive;
private javax.swing.JComboBox<String> cmbUserType;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lblADminGUI;
private javax.swing.JLabel lblName;
private javax.swing.JLabel lblSurname;
private javax.swing.JLabel lblUserActive;
private javax.swing.JLabel lblUserPassword;
private javax.swing.JLabel lblUserType;
private javax.swing.JLabel lblUsername;
private javax.swing.JTable tblUsers;
private javax.swing.JTextField txtFieldName;
private javax.swing.JTextField txtFieldPassword;
private javax.swing.JTextField txtFieldSurname;
private javax.swing.JTextField txtFieldUsername;
// End of variables declaration//GEN-END:variables
}