-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathUserPanel.java
More file actions
160 lines (140 loc) · 6.49 KB
/
Copy pathUserPanel.java
File metadata and controls
160 lines (140 loc) · 6.49 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
package org.example.ui.panels;
import org.example.database.entity.Users;
import org.example.database.service.UserService;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;
public class UserPanel extends JPanel {
private UserService userService;
private JTable userTable;
private DefaultTableModel tableModel;
private JTextField nameField, emailField, idField;
private JButton addButton, updateButton, deleteButton, refreshButton, findButton;
public UserPanel(UserService userService) {
this.userService = userService;
setLayout(new BorderLayout());
JPanel inputPanel = new JPanel(new GridLayout(4, 2));
inputPanel.setBorder(BorderFactory.createTitledBorder("User Information"));
inputPanel.add(new JLabel("ID (for update/delete):"));
idField = new JTextField();
inputPanel.add(idField);
inputPanel.add(new JLabel("Name:"));
nameField = new JTextField();
inputPanel.add(nameField);
inputPanel.add(new JLabel("Email:"));
emailField = new JTextField();
inputPanel.add(emailField);
addButton = new JButton("Add User");
updateButton = new JButton("Update User");
deleteButton = new JButton("Delete User");
findButton = new JButton("Find User by ID");
refreshButton = new JButton("Refresh All Users");
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
buttonPanel.add(findButton);
buttonPanel.add(refreshButton);
add(inputPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
tableModel = new DefaultTableModel(new Object[]{"ID", "Name", "Email"}, 0);
userTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(userTable);
add(scrollPane, BorderLayout.CENTER);
addButton.addActionListener(e -> addUser());
updateButton.addActionListener(e -> updateUser());
deleteButton.addActionListener(e -> deleteUser());
findButton.addActionListener(e -> findUserById());
refreshButton.addActionListener(e -> loadUsers());
loadUsers();
}
private void addUser() {
try {
String name = nameField.getText();
String email = emailField.getText();
if (name.isEmpty() || email.isEmpty()) {
JOptionPane.showMessageDialog(this, "Name and Email cannot be empty.", "Input Error", JOptionPane.ERROR_MESSAGE);
return;
}
userService.createUser(name, email);
JOptionPane.showMessageDialog(this, "User added successfully!");
clearFields();
loadUsers();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error adding user: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void updateUser() {
try {
Long id = Long.valueOf(idField.getText());
String name = nameField.getText();
String email = emailField.getText();
if (name.isEmpty() || email.isEmpty()) {
JOptionPane.showMessageDialog(this, "Name and Email cannot be empty.", "Input Error", JOptionPane.ERROR_MESSAGE);
return;
}
Users updatedUser = userService.updateUser(id, name, email);
if (updatedUser != null) {
JOptionPane.showMessageDialog(this, "User updated successfully!");
clearFields();
loadUsers();
} else {
JOptionPane.showMessageDialog(this, "User not found with ID: " + id, "Error", JOptionPane.ERROR_MESSAGE);
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter a valid ID.", "Input Error", JOptionPane.ERROR_MESSAGE);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error updating user: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void deleteUser() {
try {
Long id = Long.valueOf(idField.getText());
int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete user with ID: " + id + "?", "Confirm Delete", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
userService.deleteUser(id);
JOptionPane.showMessageDialog(this, "User deleted successfully!");
clearFields();
loadUsers();
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter a valid ID.", "Input Error", JOptionPane.ERROR_MESSAGE);
}
catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error deleting user: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void findUserById() {
try {
Long id = Long.valueOf(idField.getText());
Users user = userService.getUserById(id);
if (user != null) {
tableModel.setRowCount(0);
tableModel.addRow(new Object[]{user.getId(), user.getNama(), user.getEmail()});
nameField.setText(user.getNama());
emailField.setText(user.getEmail());
} else {
JOptionPane.showMessageDialog(this, "User not found with ID: " + id, "Not Found", JOptionPane.INFORMATION_MESSAGE);
clearFields();
loadUsers();
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter a valid ID.", "Input Error", JOptionPane.ERROR_MESSAGE);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error finding user: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void loadUsers() {
tableModel.setRowCount(0);
List<Users> users = userService.getAllUsers();
for (Users user : users) {
tableModel.addRow(new Object[]{user.getId(), user.getNama(), user.getEmail()});
}
}
private void clearFields() {
idField.setText("");
nameField.setText("");
emailField.setText("");
}
}