-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddWindow.java
More file actions
176 lines (152 loc) · 6.83 KB
/
AddWindow.java
File metadata and controls
176 lines (152 loc) · 6.83 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
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class AddWindow extends JFrame {
private String entityType;
private ListWindow parentWindow;
private JTextField[] textFields;
private String[] fieldNames;
public AddWindow(String entityType, ListWindow parentWindow) {
this.entityType = entityType;
this.parentWindow = parentWindow;
setTitle("Add " + entityType);
setSize(400, 300);
setLocationRelativeTo(parentWindow);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
initializeComponents();
}
private void initializeComponents() {
setLayout(new BorderLayout());
// Title panel
JPanel titlePanel = new JPanel();
titlePanel.setBackground(new Color(70, 130, 180));
JLabel titleLabel = new JLabel("Add New " + entityType);
titleLabel.setFont(new Font("Arial", Font.BOLD, 18));
titleLabel.setForeground(Color.WHITE);
titlePanel.add(titleLabel);
add(titlePanel, BorderLayout.NORTH);
// Form panel
JPanel formPanel = new JPanel(new GridBagLayout());
formPanel.setBackground(Color.WHITE);
formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.anchor = GridBagConstraints.WEST;
// Get field names based on entity type
fieldNames = getFieldNames();
textFields = new JTextField[fieldNames.length];
for (int i = 0; i < fieldNames.length; i++) {
gbc.gridx = 0;
gbc.gridy = i;
JLabel label = new JLabel(fieldNames[i] + ":");
label.setFont(new Font("Arial", Font.PLAIN, 12));
formPanel.add(label, gbc);
gbc.gridx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
textFields[i] = new JTextField(20);
textFields[i].setFont(new Font("Arial", Font.PLAIN, 12));
formPanel.add(textFields[i], gbc);
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
}
add(formPanel, BorderLayout.CENTER);
// Button panel
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setBackground(new Color(245, 245, 245));
JButton saveBtn = new JButton("Save");
saveBtn.setBackground(new Color(60, 179, 113));
saveBtn.setForeground(Color.WHITE);
saveBtn.setFont(new Font("Arial", Font.BOLD, 12));
saveBtn.setFocusPainted(false);
saveBtn.addActionListener(e -> saveRecord());
JButton cancelBtn = new JButton("Cancel");
cancelBtn.setBackground(new Color(220, 20, 60));
cancelBtn.setForeground(Color.WHITE);
cancelBtn.setFont(new Font("Arial", Font.BOLD, 12));
cancelBtn.setFocusPainted(false);
cancelBtn.addActionListener(e -> dispose());
buttonPanel.add(saveBtn);
buttonPanel.add(cancelBtn);
add(buttonPanel, BorderLayout.SOUTH);
}
private String[] getFieldNames() {
switch (entityType) {
case "Doctor":
return new String[]{"Name", "Specialization", "Phone", "Email"};
case "Patient":
return new String[]{"Name", "Age", "Phone", "Address"};
case "Appointment":
return new String[]{"Patient Name", "Doctor Name", "Date (YYYY-MM-DD)", "Time (HH:MM)"};
case "Department":
return new String[]{"Name", "Head Doctor", "Location"};
default:
return new String[]{};
}
}
private void saveRecord() {
// Validate input
for (int i = 0; i < textFields.length; i++) {
if (textFields[i].getText().trim().isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill in all fields.");
return;
}
}
try (Connection conn = DatabaseConnection.getConnection()) {
String query = getInsertQuery();
PreparedStatement pstmt = conn.prepareStatement(query);
// Set parameters based on entity type
setParameters(pstmt);
int result = pstmt.executeUpdate();
if (result > 0) {
JOptionPane.showMessageDialog(this, entityType + " added successfully!");
parentWindow.refreshData();
dispose();
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Error saving record: " + e.getMessage());
e.printStackTrace();
}
}
private String getInsertQuery() {
switch (entityType) {
case "Doctor":
return "INSERT INTO doctors (name, specialization, phone, email) VALUES (?, ?, ?, ?)";
case "Patient":
return "INSERT INTO patients (name, age, phone, address) VALUES (?, ?, ?, ?)";
case "Appointment":
return "INSERT INTO appointments (patient_name, doctor_name, appointment_date, appointment_time) VALUES (?, ?, ?, ?)";
case "Department":
return "INSERT INTO departments (name, head_doctor, location) VALUES (?, ?, ?)";
default:
return "";
}
}
private void setParameters(PreparedStatement pstmt) throws SQLException {
switch (entityType) {
case "Doctor":
pstmt.setString(1, textFields[0].getText().trim());
pstmt.setString(2, textFields[1].getText().trim());
pstmt.setString(3, textFields[2].getText().trim());
pstmt.setString(4, textFields[3].getText().trim());
break;
case "Patient":
pstmt.setString(1, textFields[0].getText().trim());
pstmt.setInt(2, Integer.parseInt(textFields[1].getText().trim()));
pstmt.setString(3, textFields[2].getText().trim());
pstmt.setString(4, textFields[3].getText().trim());
break;
case "Appointment":
pstmt.setString(1, textFields[0].getText().trim());
pstmt.setString(2, textFields[1].getText().trim());
pstmt.setDate(3, Date.valueOf(textFields[2].getText().trim()));
pstmt.setTime(4, Time.valueOf(textFields[3].getText().trim() + ":00"));
break;
case "Department":
pstmt.setString(1, textFields[0].getText().trim());
pstmt.setString(2, textFields[1].getText().trim());
pstmt.setString(3, textFields[2].getText().trim());
break;
}
}
}