-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeListWindow.java
More file actions
253 lines (211 loc) · 10.1 KB
/
EmployeeListWindow.java
File metadata and controls
253 lines (211 loc) · 10.1 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
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
public class EmployeeListWindow {
private JFrame frame;
private JTable table;
private DefaultTableModel model;
private JButton addButton;
public EmployeeListWindow() {
frame = new JFrame("Employee List");
frame.setSize(700, 450);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon icon = new ImageIcon("C:/Users/Lynsy/OneDrive/Desktop/Programming/Java programs/Employee LogIn/bluegame.png");
frame.setIconImage(icon.getImage());
// Use the correct image path
String imagePath = "C:/Users/Lynsy/OneDrive/Desktop/Programming/Java programs/Employee LogIn/Sky Gradient.jpg";
BackgroundPanel panel = new BackgroundPanel(imagePath);
panel.setLayout(new BorderLayout());
frame.setContentPane(panel);
// Table setup
model = new DefaultTableModel(new Object[]{"ID", "Name", "Role", "Salary", ""}, 0) {
public boolean isCellEditable(int row, int column) {
return column == 4; // Only delete button column is editable
}
};
table = new JTable(model);
table.setFont(new Font("Verdana", Font.PLAIN, 13));
table.setRowHeight(30);
table.getColumnModel().getColumn(4).setCellRenderer(new ButtonRenderer());
table.getColumnModel().getColumn(4).setCellEditor(new ButtonEditor(new JCheckBox()));
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
// Add Employee Button
addButton = new JButton("Add Employee");
addButton.setFont(new Font("Verdana", Font.BOLD, 14));
addButton.setBackground(new Color(16, 52, 166));
addButton.setForeground(Color.WHITE);
addButton.setFocusPainted(false);
addButton.setPreferredSize(new Dimension(180, 40));
JPanel topPanel = new JPanel();
topPanel.setOpaque(false); // Make top panel transparent for background visibility
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 15));
topPanel.add(addButton);
panel.add(topPanel, BorderLayout.NORTH);
addButton.addActionListener(e -> showAddEmployeeDialog());
frame.setVisible(true);
}
private void showAddEmployeeDialog() {
JTextField idField = new JTextField();
JTextField nameField = new JTextField();
JTextField salaryField = new JTextField();
JComboBox<String> positionBox = new JComboBox<>(new String[]{"Project Manager", "Developer", "Analyst", "Designer", "Tester"});
idField.setFont(new Font("Verdana", Font.PLAIN, 13));
nameField.setFont(new Font("Verdana", Font.PLAIN, 13));
salaryField.setFont(new Font("Verdana", Font.PLAIN, 13));
positionBox.setFont(new Font("Verdana", Font.PLAIN, 13));
((AbstractDocument) idField.getDocument()).setDocumentFilter(new NumericDocumentFilter());
((AbstractDocument) salaryField.getDocument()).setDocumentFilter(new NumericDocumentFilter());
((AbstractDocument) nameField.getDocument()).setDocumentFilter(new AlphaDocumentFilter());
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.setOpaque(false);
gbc.gridx = 0; gbc.gridy = 0;
panel.add(new JLabel("ID Number:") {{ setFont(new Font("Verdana", Font.BOLD, 13)); }}, gbc);
gbc.gridx = 1;
panel.add(idField, gbc);
gbc.gridx = 0; gbc.gridy = 1;
panel.add(new JLabel("Name:") {{ setFont(new Font("Verdana", Font.BOLD, 13)); }}, gbc);
gbc.gridx = 1;
panel.add(nameField, gbc);
gbc.gridx = 0; gbc.gridy = 2;
panel.add(new JLabel("Role:") {{ setFont(new Font("Verdana", Font.BOLD, 13)); }}, gbc);
gbc.gridx = 1;
panel.add(positionBox, gbc);
gbc.gridx = 0; gbc.gridy = 3;
panel.add(new JLabel("Salary:") {{ setFont(new Font("Verdana", Font.BOLD, 13)); }}, gbc);
gbc.gridx = 1;
panel.add(salaryField, gbc);
JButton okButton = new JButton("OK");
okButton.setFont(new Font("Verdana", Font.BOLD, 13));
okButton.setBackground(new Color(16, 52, 166));
okButton.setForeground(Color.WHITE);
JPanel bottomPanel = new JPanel();
bottomPanel.setOpaque(false); // Make bottom panel transparent for background visibility
bottomPanel.add(okButton);
// Use the BackgroundPanel in the dialog
String imagePath = "C:/Users/Lynsy/OneDrive/Desktop/Programming/Java programs/Employee LogIn/Sky Gradient.jpg";
BackgroundPanel dialogPanel = new BackgroundPanel(imagePath);
dialogPanel.setLayout(new BorderLayout());
dialogPanel.add(panel, BorderLayout.CENTER);
dialogPanel.add(bottomPanel, BorderLayout.SOUTH);
JDialog dialog = new JDialog(frame, "Add Employee Info", true);
dialog.setContentPane(dialogPanel);
dialog.setSize(400, 300);
dialog.setLocationRelativeTo(frame);
okButton.addActionListener(e -> {
String id = idField.getText().trim();
String name = nameField.getText().trim();
String role = (String) positionBox.getSelectedItem();
String salary = salaryField.getText().trim();
if (id.isEmpty() || name.isEmpty() || salary.isEmpty()) {
JOptionPane.showMessageDialog(dialog, "Please fill out all fields.");
return;
}
model.addRow(new Object[]{id, name, role, salary, "X"});
dialog.dispose();
});
dialog.setVisible(true);
}
class NumericDocumentFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
if (string != null && string.matches("\\d+")) {
super.insertString(fb, offset, string, attr);
} else {
showError("Only numbers are allowed!");
}
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
if (text != null && text.matches("\\d+")) {
super.replace(fb, offset, length, text, attrs);
} else {
showError("Only numbers are allowed!");
}
}
private void showError(String msg) {
JOptionPane.showMessageDialog(null, msg, "Input Error", JOptionPane.ERROR_MESSAGE);
}
}
class AlphaDocumentFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
if (string != null && string.matches("[a-zA-Z\\s]+")) {
super.insertString(fb, offset, string, attr);
} else {
showError("Only letters are allowed!");
}
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
if (text != null && text.matches("[a-zA-Z\\s]+")) {
super.replace(fb, offset, length, text, attrs);
} else {
showError("Only letters are allowed!");
}
}
private void showError(String msg) {
JOptionPane.showMessageDialog(null, msg, "Input Error", JOptionPane.ERROR_MESSAGE);
}
}
class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setText("X");
setFont(new Font("Verdana", Font.BOLD, 12));
setForeground(Color.WHITE);
setBackground(new Color(200, 80, 80));
setFocusPainted(false);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
return this;
}
}
class ButtonEditor extends DefaultCellEditor {
protected JButton button;
private int selectedRow;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton("X");
button.setFont(new Font("Verdana", Font.BOLD, 12));
button.setForeground(Color.WHITE);
button.setBackground(new Color(200, 80, 80));
button.setFocusPainted(false);
button.addActionListener(e -> model.removeRow(selectedRow));
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
selectedRow = row;
return button;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(EmployeeListWindow::new);
}
}
// BackgroundPanel class - uses imagePath correctly
class BackgroundPanel extends JPanel {
private Image backgroundImage;
public BackgroundPanel(String imagePath) {
ImageIcon icon = new ImageIcon(imagePath);
if (icon.getImageLoadStatus() == MediaTracker.COMPLETE) {
backgroundImage = icon.getImage();
} else {
JOptionPane.showMessageDialog(this, "Could not load background image.", "Image Error", JOptionPane.ERROR_MESSAGE);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
}
}
}