private JPanel createAddFlightCard(int adminId) {
JPanel outerPanel = new JPanel(new GridBagLayout());
outerPanel.setBackground(new Color(220, 240, 255));
JPanel contentPanel = new JPanel(new GridBagLayout());
contentPanel.setBackground(new Color(250, 250, 255));
contentPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(20, 20, 20, 20),
BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(new Color(180, 190, 200), 1),
"Add New Flight",
TitledBorder.CENTER, TitledBorder.TOP,
new Font("Arial", Font.BOLD, 16), new Color(50, 70, 90)
)
));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(8, 8, 8, 8);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
// Validation flags for the button
final boolean[] isValidPlaneSelection = {false};
final boolean[] isValidSourceDest = {false};
final boolean[] isValidReportingDateTime = {false};
final boolean[] isValidArrivalDateTime = {false};
final boolean[] isValidExpense = {false};
// Plane Selection
gbc.gridx = 0;
gbc.gridy = 0;
contentPanel.add(new JLabel("Select Plane:"), gbc);
gbc.gridx = 1;
JComboBox<String> planeComboBox = new JComboBox<>();
loadAdminPlanes(planeComboBox, adminId);
contentPanel.add(planeComboBox, gbc);
// Source Airport
gbc.gridx = 0;
gbc.gridy = 1;
contentPanel.add(new JLabel("Source Airport:"), gbc);
gbc.gridx = 1;
JComboBox<String> sourceComboBox = new JComboBox<>();
loadAirports(sourceComboBox);
contentPanel.add(sourceComboBox, gbc);
// Destination Airport
gbc.gridx = 0;
gbc.gridy = 2;
contentPanel.add(new JLabel("Destination Airport:"), gbc);
gbc.gridx = 1;
JComboBox<String> destComboBox = new JComboBox<>();
loadAirports(destComboBox);
contentPanel.add(destComboBox, gbc);
// Reporting Date and Time
gbc.gridx = 0;
gbc.gridy = 3;
contentPanel.add(new JLabel("Reporting Date & Time:"), gbc);
gbc.gridx = 1;
JPanel reportingPanel = new JPanel(new GridBagLayout());
reportingPanel.setOpaque(false);
GridBagConstraints repGbc = new GridBagConstraints();
repGbc.insets = new Insets(0, 2, 0, 2);
repGbc.anchor = GridBagConstraints.WEST;
Calendar cal = Calendar.getInstance();
int currentYear = cal.get(Calendar.YEAR);
int currentMonth = cal.get(Calendar.MONTH) + 1;
int currentDay = cal.get(Calendar.DAY_OF_MONTH);
// Reporting Date Components
JComboBox<Integer> repDayComboBox = new JComboBox<>();
JComboBox<Integer> repMonthComboBox = new JComboBox<>();
JComboBox<Integer> repYearComboBox = new JComboBox<>();
for (int i = currentYear; i <= currentYear + 5; i++) {
repYearComboBox.addItem(i);
}
for (int i = 1; i <= 12; i++) {
repMonthComboBox.addItem(i);
}
repMonthComboBox.setSelectedItem(currentMonth);
updateDays(repYearComboBox, repMonthComboBox, repDayComboBox, currentDay);
repDayComboBox.setSelectedItem(currentDay);
// Reporting Time Components
JComboBox<Integer> repHour = new JComboBox<>();
JComboBox<Integer> repMinute = new JComboBox<>();
JComboBox<Integer> repSecond = new JComboBox<>();
populateComboBox(repHour, 0, 23);
populateComboBox(repMinute, 0, 59);
populateComboBox(repSecond, 0, 59);
// Add components to reporting panel
repGbc.gridx = 0; repGbc.gridy = 0; reportingPanel.add(new JLabel("D:"), repGbc);
repGbc.gridx = 1; reportingPanel.add(repDayComboBox, repGbc);
repGbc.gridx = 2; reportingPanel.add(new JLabel("M:"), repGbc);
repGbc.gridx = 3; reportingPanel.add(repMonthComboBox, repGbc);
repGbc.gridx = 4; reportingPanel.add(new JLabel("Y:"), repGbc);
repGbc.gridx = 5; reportingPanel.add(repYearComboBox, repGbc);
repGbc.gridx = 6; reportingPanel.add(new JLabel(" Time:"), repGbc);
repGbc.gridx = 7; reportingPanel.add(repHour, repGbc);
repGbc.gridx = 8; reportingPanel.add(new JLabel(":"), repGbc);
repGbc.gridx = 9; reportingPanel.add(repMinute, repGbc);
repGbc.gridx = 10; reportingPanel.add(new JLabel(":"), repGbc);
repGbc.gridx = 11; reportingPanel.add(repSecond, repGbc);
contentPanel.add(reportingPanel, gbc);
// Arrival Date and Time
gbc.gridx = 0;
gbc.gridy = 4;
contentPanel.add(new JLabel("Arrival Date & Time:"), gbc);
gbc.gridx = 1;
JPanel arrivalPanel = new JPanel(new GridBagLayout());
arrivalPanel.setOpaque(false);
GridBagConstraints arrGbc = new GridBagConstraints();
arrGbc.insets = new Insets(0, 2, 0, 2);
arrGbc.anchor = GridBagConstraints.WEST;
// Arrival Date Components
JComboBox<Integer> arrDayComboBox = new JComboBox<>();
JComboBox<Integer> arrMonthComboBox = new JComboBox<>();
JComboBox<Integer> arrYearComboBox = new JComboBox<>();
for (int i = currentYear; i <= currentYear + 5; i++) {
arrYearComboBox.addItem(i);
}
for (int i = 1; i <= 12; i++) {
arrMonthComboBox.addItem(i);
}
arrMonthComboBox.setSelectedItem(currentMonth);
updateDays(arrYearComboBox, arrMonthComboBox, arrDayComboBox, currentDay);
arrDayComboBox.setSelectedItem(currentDay);
// Arrival Time Components
JComboBox<Integer> arrHour = new JComboBox<>();
JComboBox<Integer> arrMinute = new JComboBox<>();
JComboBox<Integer> arrSecond = new JComboBox<>();
populateComboBox(arrHour, 0, 23);
populateComboBox(arrMinute, 0, 59);
populateComboBox(arrSecond, 0, 59);
// Add components to arrival panel
arrGbc.gridx = 0; arrGbc.gridy = 0; arrivalPanel.add(new JLabel("D:"), arrGbc);
arrGbc.gridx = 1; arrivalPanel.add(arrDayComboBox, arrGbc);
arrGbc.gridx = 2; arrivalPanel.add(new JLabel("M:"), arrGbc);
arrGbc.gridx = 3; arrivalPanel.add(arrMonthComboBox, arrGbc);
arrGbc.gridx = 4; arrivalPanel.add(new JLabel("Y:"), arrGbc);
arrGbc.gridx = 5; arrivalPanel.add(arrYearComboBox, arrGbc);
arrGbc.gridx = 6; arrivalPanel.add(new JLabel(" Time:"), arrGbc);
arrGbc.gridx = 7; arrivalPanel.add(arrHour, arrGbc);
arrGbc.gridx = 8; arrivalPanel.add(new JLabel(":"), arrGbc);
arrGbc.gridx = 9; arrivalPanel.add(arrMinute, arrGbc);
arrGbc.gridx = 10; arrivalPanel.add(new JLabel(":"), arrGbc);
arrGbc.gridx = 11; arrivalPanel.add(arrSecond, arrGbc);
contentPanel.add(arrivalPanel, gbc);
// Expense
gbc.gridx = 0;
gbc.gridy = 5;
contentPanel.add(new JLabel("Expense:"), gbc);
gbc.gridx = 1;
JTextField expenseField = new JTextField(15);
contentPanel.add(expenseField, gbc);
// Add Flight Button
gbc.gridx = 0;
gbc.gridy = 6;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
addFlightButton = createStyledButton("Add Flight", null);
addFlightButton.setEnabled(false);
contentPanel.add(addFlightButton, gbc);
// --- Add Listeners for Dynamic Validation ---
// Listener for plane selection
planeComboBox.addItemListener(e -> {
isValidPlaneSelection[0] = e.getItem() != null && !((String) e.getItem()).startsWith("No planes");
updateAddFlightButtonState(isValidPlaneSelection[0], isValidSourceDest[0], isValidReportingDateTime[0], isValidArrivalDateTime[0], isValidExpense[0]);
});
// Listener for source/dest combo boxes
ItemListener sourceDestListener = e -> {
boolean valid = sourceComboBox.getSelectedItem() != null && destComboBox.getSelectedItem() != null && !sourceComboBox.getSelectedItem().equals(destComboBox.getSelectedItem());
isValidSourceDest[0] = valid;
updateAddFlightButtonState(isValidPlaneSelection[0], isValidSourceDest[0], isValidReportingDateTime[0], isValidArrivalDateTime[0], isValidExpense[0]);
};
sourceComboBox.addItemListener(sourceDestListener);
destComboBox.addItemListener(sourceDestListener);
// Listener for reporting date components
ActionListener reportingDateComponentListener = e -> {
Integer selectedDay = (Integer) repDayComboBox.getSelectedItem();
if (selectedDay == null) selectedDay = currentDay;
updateDays(repYearComboBox, repMonthComboBox, repDayComboBox, selectedDay);
isValidReportingDateTime[0] = isDateTimeValid(repYearComboBox, repMonthComboBox, repDayComboBox, repHour, repMinute, repSecond);
validateDateTimeOrder(repYearComboBox, repMonthComboBox, repDayComboBox, repHour, repMinute, repSecond,
arrYearComboBox, arrMonthComboBox, arrDayComboBox, arrHour, arrMinute, arrSecond,
isValidReportingDateTime, isValidArrivalDateTime);
updateAddFlightButtonState(isValidPlaneSelection[0], isValidSourceDest[0], isValidReportingDateTime[0], isValidArrivalDateTime[0], isValidExpense[0]);
};
repYearComboBox.addActionListener(reportingDateComponentListener);
repMonthComboBox.addActionListener(reportingDateComponentListener);
repDayComboBox.addActionListener(reportingDateComponentListener);
// Listener for arrival date components
ActionListener arrivalDateComponentListener = e -> {
Integer selectedDay = (Integer) arrDayComboBox.getSelectedItem();
if (selectedDay == null) selectedDay = currentDay;
updateDays(arrYearComboBox, arrMonthComboBox, arrDayComboBox, selectedDay);
isValidArrivalDateTime[0] = isDateTimeValid(arrYearComboBox, arrMonthComboBox, arrDayComboBox, arrHour, arrMinute, arrSecond);
validateDateTimeOrder(repYearComboBox, repMonthComboBox, repDayComboBox, repHour, repMinute, repSecond,
arrYearComboBox, arrMonthComboBox, arrDayComboBox, arrHour, arrMinute, arrSecond,
isValidReportingDateTime, isValidArrivalDateTime);
updateAddFlightButtonState(isValidPlaneSelection[0], isValidSourceDest[0], isValidReportingDateTime[0], isValidArrivalDateTime[0], isValidExpense[0]);
};
arrYearComboBox.addActionListener(arrivalDateComponentListener);
arrMonthComboBox.addActionListener(arrivalDateComponentListener);
arrDayComboBox.addActionListener(arrivalDateComponentListener);
// Listener for reporting time components
ActionListener reportingTimeComponentListener = e -> {
isValidReportingDateTime[0] = isDateTimeValid(repYearComboBox, repMonthComboBox, repDayComboBox, repHour, repMinute, repSecond);
validateDateTimeOrder(repYearComboBox, repMonthComboBox, repDayComboBox, repHour, repMinute, repSecond,
arrYearComboBox, arrMonthComboBox, arrDayComboBox, arrHour, arrMinute, arrSecond,
isValidReportingDateTime, isValidArrivalDateTime);
updateAddFlightButtonState(isValidPlaneSelection[0], isValidSourceDest[0], isValidReportingDateTime[0], isValidArrivalDateTime[0], isValidExpense[0]);
};
repHour.addActionListener(reportingTimeComponentListener);
repMinute.addActionListener(reportingTimeComponentListener);
repSecond.addActionListener(reportingTimeComponentListener);
// Listener for arrival time components
ActionListener arrivalTimeComponentListener = e -> {
isValidArrivalDateTime[0] = isDateTimeValid(arrYearComboBox, arrMonthComboBox, arrDayComboBox, arrHour, arrMinute, arrSecond);
validateDateTimeOrder(repYearComboBox, repMonthComboBox, repDayComboBox, repHour, repMinute, repSecond,
arrYearComboBox, arrMonthComboBox, arrDayComboBox, arrHour, arrMinute, arrSecond,
isValidReportingDateTime, isValidArrivalDateTime);
updateAddFlightButtonState(isValidPlaneSelection[0], isValidSourceDest[0], isValidReportingDateTime[0], isValidArrivalDateTime[0], isValidExpense[0]);
};
arrHour.addActionListener(arrivalTimeComponentListener);
arrMinute.addActionListener(arrivalTimeComponentListener);
arrSecond.addActionListener(arrivalTimeComponentListener);
// DocumentListener for expense field
expenseField.getDocument().addDocumentListener(new DocumentListener() {
private void update() {
try {
float expense = Float.parseFloat(expenseField.getText().trim());
isValidExpense[0] = expense > 0;
} catch (NumberFormatException ex) {
isValidExpense[0] = false;
}
updateAddFlightButtonState(isValidPlaneSelection[0], isValidSourceDest[0], isValidReportingDateTime[0], isValidArrivalDateTime[0], isValidExpense[0]);
}
@Override public void insertUpdate(DocumentEvent e) { update(); }
@Override public void removeUpdate(DocumentEvent e) { update(); }
@Override public void changedUpdate(DocumentEvent e) {}
});
// Set action listener for the button AFTER validation logic is in place
addFlightButton.addActionListener(e -> {
// Final validation check before execution
if (!isValidPlaneSelection[0] || !isValidSourceDest[0] || !isValidReportingDateTime[0] || !isValidArrivalDateTime[0] || !isValidExpense[0]) {
JOptionPane.showMessageDialog(frame, "Please fill out all fields correctly to add a flight.", "Validation Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
String selectedPlane = (String) planeComboBox.getSelectedItem();
int planeId = Integer.parseInt(selectedPlane.split(" - ")[0]);
String source = (String) sourceComboBox.getSelectedItem();
String destination = (String) destComboBox.getSelectedItem();
Timestamp reportingTimestamp = getDateTimeFromPickers(repYearComboBox, repMonthComboBox, repDayComboBox, repHour, repMinute, repSecond);
Timestamp arrivalTimestamp = getDateTimeFromPickers(arrYearComboBox, arrMonthComboBox, arrDayComboBox, arrHour, arrMinute, arrSecond);
// This check is redundant with listeners but good for final validation
if (!arrivalTimestamp.after(reportingTimestamp)) {
JOptionPane.showMessageDialog(frame, "Arrival time must be after reporting time.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
float expense = Float.parseFloat(expenseField.getText().trim());
addFlight(connection, planeId, source, destination, arrivalTimestamp, reportingTimestamp, expense);
JOptionPane.showMessageDialog(frame, "Flight added successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
// Reset fields
expenseField.setText("");
repYearComboBox.setSelectedItem(currentYear);
repMonthComboBox.setSelectedItem(currentMonth);
updateDays(repYearComboBox, repMonthComboBox, repDayComboBox, currentDay);
repDayComboBox.setSelectedItem(currentDay);
repHour.setSelectedIndex(0);
repMinute.setSelectedIndex(0);
repSecond.setSelectedIndex(0);
arrYearComboBox.setSelectedItem(currentYear);
arrMonthComboBox.setSelectedItem(currentMonth);
updateDays(arrYearComboBox, arrMonthComboBox, arrDayComboBox, currentDay);
arrDayComboBox.setSelectedItem(currentDay);
arrHour.setSelectedIndex(0);
arrMinute.setSelectedIndex(0);
arrSecond.setSelectedIndex(0);
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame, "Error adding flight: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
});
GridBagConstraints outerGbc = new GridBagConstraints();
outerGbc.gridx = 0;
outerGbc.gridy = 0;
outerGbc.weightx = 1.0;
outerGbc.weighty = 1.0;
outerGbc.anchor = GridBagConstraints.CENTER;
outerPanel.add(contentPanel, outerGbc);
// Initial validation check after the GUI is built
SwingUtilities.invokeLater(() -> {
isValidPlaneSelection[0] = planeComboBox.getSelectedItem() != null && !((String) planeComboBox.getSelectedItem()).startsWith("No planes");
isValidSourceDest[0] = sourceComboBox.getSelectedItem() != null && destComboBox.getSelectedItem() != null && !sourceComboBox.getSelectedItem().equals(destComboBox.getSelectedItem());
isValidReportingDateTime[0] = isDateTimeValid(repYearComboBox, repMonthComboBox, repDayComboBox, repHour, repMinute, repSecond);
isValidArrivalDateTime[0] = isDateTimeValid(arrYearComboBox, arrMonthComboBox, arrDayComboBox, arrHour, arrMinute, arrSecond);
try { isValidExpense[0] = Float.parseFloat(expenseField.getText().trim()) > 0; } catch (NumberFormatException ignored) {}
validateDateTimeOrder(repYearComboBox, repMonthComboBox, repDayComboBox, repHour, repMinute, repSecond,
arrYearComboBox, arrMonthComboBox, arrDayComboBox, arrHour, arrMinute, arrSecond,
isValidReportingDateTime, isValidArrivalDateTime);
updateAddFlightButtonState(isValidPlaneSelection[0], isValidSourceDest[0], isValidReportingDateTime[0], isValidArrivalDateTime[0], isValidExpense[0]);
});
return outerPanel;
}
i just want you to add a logic just it should allow me to add a flight whose time is in past from on time