private JPanel createAddFlightCard(int adminId) { JPanel outerPanel = new JPanel(new…
- User
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
Assistant
Looking at your code, I can see that currently the year combo boxes only allow selection from the current year onwards. To allow adding flights with past times, you need to modify the year range to include past years. Here's the updated code with the necessary changes:
// Reporting Date Components JComboBox<Integer> repDayComboBox = new JComboBox<>(); JComboBox<Integer> repMonthComboBox = new JComboBox<>(); JComboBox<Integer> repYearComboBox = new JComboBox<>(); // Allow selection of past years (10 years back from current year) for (int i = currentYear - 10; i <= currentYear + 5; i++) { repYearComboBox.addItem(i); } for (int i = 1; i <= 12; i++) { repMonthComboBox.addItem(i); } repMonthComboBox.setSelectedItem(currentMonth); repYearComboBox.setSelectedItem(currentYear); // Set current year as default updateDays(repYearComboBox, repMonthComboBox, repDayComboBox, currentDay); repDayComboBox.setSelectedItem(currentDay);And similarly for the arrival date components:
// Arrival Date Components JComboBox<Integer> arrDayComboBox = new JComboBox<>(); JComboBox<Integer> arrMonthComboBox = new JComboBox<>(); JComboBox<Integer> arrYearComboBox = new JComboBox<>(); // Allow selection of past years (10 years back from current year) for (int i = currentYear - 10; i <= currentYear + 5; i++) { arrYearComboBox.addItem(i); } for (int i = 1; i <= 12; i++) { arrMonthComboBox.addItem(i); } arrMonthComboBox.setSelectedItem(currentMonth); arrYearComboBox.setSelectedItem(currentYear); // Set current year as default updateDays(arrYearComboBox, arrMonthComboBox, arrDayComboBox, currentDay); arrDayComboBox.setSelectedItem(currentDay);The key changes are:
- Changed
for (int i = currentYear; i <= currentYear + 5; i++)tofor (int i = currentYear - 10; i <= currentYear + 5; i++)for both reporting and arrival year combo boxes - Added
repYearComboBox.setSelectedItem(currentYear);andarrYearComboBox.setSelectedItem(currentYear);to set the current year as the default selection
This will allow users to select dates up to 10 years in the past while still defaulting to the current date. The existing validation that ensures arrival time is after reporting time will still work correctly, whether the times are in the past or future.
- Changed
- User
sorry it ahould't allow me to create flights in past we can only add post flights from ongoing time and make sure that the arrival time must be minimum after one hour and also you can add document listner which can show message below like these and also add that document listner for expense text field modelField.getDocument().addDocumentListener(new DocumentListener() { private void update() { String text = modelField.getText(); boolean matches = text.matches("[A-Z0-9]+"); if (text.isEmpty()) { modelFeedback.setText(" Please enter plane model. "); modelFeedback.setForeground(Color.RED); isValidModel[0] = false; } else if (matches) { modelFeedback.setText("✓ Valid. "); modelFeedback.setForeground(new Color(0, 150, 0)); isValidModel[0] = true; } else { modelFeedback.setText("✘ Only uppercase letters and numbers allowed."); modelFeedback.setForeground(Color.RED); isValidModel[0] = false; } updateAddPlaneButtonState(addPlaneButton, isValidModel[0], isValidManufacturer[0], isValidBusinessSeats[0], isValidEconomySeats[0]); } @Override public void insertUpdate(DocumentEvent e) { update(); } @Override public void removeUpdate(DocumentEvent e) { update(); } @Override public void changedUpdate(DocumentEvent e) {} });
Assistant
Here's the updated code with the requested changes:
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); // Add feedback label for date/time validation gbc.gridx = 1; gbc.gridy = 5; JLabel dateTimeFeedback = new JLabel(" "); dateTimeFeedback.setFont(new Font("Arial", Font.PLAIN, 11)); contentPanel.add(dateTimeFeedback, gbc); // Expense gbc.gridx = 0; gbc.gridy = 6; contentPanel.add(new JLabel("Expense:"), gbc); gbc.gridx = 1; JTextField expenseField = new JTextField(15); contentPanel.add(expenseField, gbc); // Add feedback label for expense gbc.gridx = 1; gbc.gridy = 7; JLabel expenseFeedback = new JLabel(" "); expenseFeedback.setFont(new Font("Arial", Font.PLAIN, 11)); contentPanel.add(expenseFeedback, gbc); // Add Flight Button gbc.gridx = 0; gbc.gridy = 8; 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); // Helper method to validate date/time with enhanced rules Runnable validateDateTime = () -> { try { Timestamp currentTime = new Timestamp(System.currentTimeMillis()); Timestamp reportingTime = getDateTimeFromPickers(repYearComboBox, repMonthComboBox, repDayComboBox, repHour, repMinute, repSecond); Timestamp arrivalTime = getDateTimeFromPickers(arrYearComboBox, arrMonthComboBox, arrDayComboBox, arrHour, arrMinute, arrSecond); // Check if reporting time is in the past if (reportingTime.before(currentTime)) { dateTimeFeedback.setText("✘ Reporting time cannot be in the past."); dateTimeFeedback.setForeground(Color.RED); isValidReportingDateTime[0] = false; isValidArrivalDateTime[0] = false; } // Check if arrival time is at least 1 hour after reporting time else if ((arrivalTime.getTime() - reportingTime.getTime()) < 3600000) { // 3600000 ms = 1 hour dateTimeFeedback.setText("✘ Arrival time must be at least 1 hour after reporting time."); dateTimeFeedback.setForeground(Color.RED); isValidReportingDateTime[0] = true; isValidArrivalDateTime[0] = false; } else { dateTimeFeedback.setText("✓ Valid date/time selection."); dateTimeFeedback.setForeground(new Color(0, 150, 0)); isValidReportingDateTime[0] = true; isValidArrivalDateTime[0] = true; } } catch (Exception e) { dateTimeFeedback.setText(" "); isValidReportingDateTime[0] = false; isValidArrivalDateTime[0] = false; } updateAddFlightButtonState(isValidPlaneSelection[0], isValidSourceDest[0], isValidReportingDateTime[0], isValidArrivalDateTime[0], isValidExpense[0]); }; // Listener for reporting date components ActionListener reportingDateComponentListener = e -> { Integer selectedDay = (Integer) repDayComboBox.getSelectedItem(); if (selectedDay == null) selectedDay = currentDay; updateDays(repYearComboBox, repMonthComboBox, repDayComboBox, selectedDay); validateDateTime.run(); }; 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); validateDateTime.run(); }; arrYearComboBox.addActionListener(arrivalDateComponentListener); arrMonthComboBox.addActionListener(arrivalDateComponentListener); arrDayComboBox.addActionListener(arrivalDateComponentListener); // Listener for reporting time components ActionListener reportingTimeComponentListener = e -> validateDateTime.run(); repHour.addActionListener(reportingTimeComponentListener); repMinute.addActionListener(reportingTimeComponentListener); repSecond.addActionListener(reportingTimeComponentListener); // Listener for arrival time components ActionListener arrivalTimeComponentListener = e -> validateDateTime.run(); arrHour.addActionListener(arrivalTimeComponentListener); arrMinute.addActionListener(arrivalTimeComponentListener); arrSecond.addActionListener(arrivalTimeComponentListener); // DocumentListener for expense field with feedback expenseField.getDocument().addDocumentListener(new DocumentListener() { private void update() { String text = expenseField.getText().trim(); if (text.isEmpty()) { expenseFeedback.setText(" Please enter expense amount."); expenseFeedback.setForeground(Color.RED); isValidExpense[0] = false; } else { try { float expense = Float.parseFloat(text); if (expense <= 0) { expenseFeedback.setText("✘ Expense must be greater than 0."); expenseFeedback.setForeground(Color.RED); isValidExpense[0] = false; } else if (expense > 1000000) { expenseFeedback.setText("✘ Expense seems too high. Please verify."); expenseFeedback.setForeground(Color.ORANGE); isValidExpense[0] = true; // Still valid but with warning } else { expenseFeedback.setText("✓ Valid expense amount."); expenseFeedback.setForeground(new Color(0, 150, 0)); isValidExpense[0] = true; } } catch (NumberFormatException ex) { expenseFeedback.setText("✘ Please enter a valid number."); expenseFeedback.setForeground(Color.RED); 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); // Final validation Timestamp currentTime = new Timestamp(System.currentTimeMillis()); if (reportingTimestamp.before(currentTime)) { JOptionPane.showMessageDialog(frame, "Reporting time cannot be in the past.", "Error", JOptionPane.ERROR_MESSAGE); return; } if ((arrivalTimestamp.getTime() - reportingTimestamp.getTime()) < 3600000) { JOptionPane.showMessageDialog(frame, "Arrival time must be at least 1 hour 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(""); expenseFeedback.setText(" "); dateTimeFeedback.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()); try { isValidExpense[0] = Float.parseFloat(expenseField.getText().trim()) > 0; } catch (NumberFormatException ignored) {} validateDateTime.run(); }); return outerPanel; }Key changes made:
-
Added feedback labels: Added
dateTimeFeedbacklabel for date/time validation messages andexpenseFeedbacklabel for expense validation messages. -
Enhanced date/time validation: Created a unified
validateDateTimemethod that checks:- Reporting time cannot be in the past
- Arrival time must be at least 1 hour after reporting time
-
Added document listener for expense field with validation messages:
- Empty field warning
- Invalid number format error
- Negative or zero value error
- Warning for suspiciously high values (over 1 million)
- Success message for valid amounts
-
Updated grid positions: Adjusted the grid layout to accommodate the new feedback labels.
-
Final validation in button action: Added checks before flight creation to ensure reporting time isn't in the past and arrival time is at least 1 hour later.
The validation now provides real-time feedback to users, making the form more intuitive and preventing invalid flight entries.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.