private JPanel createAddPlaneCard() { JPanel outerPanel = new JPanel(new…
- User
private JPanel createAddPlaneCard() { 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 Plane", 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; // Plane Model gbc.gridx = 0; gbc.gridy = 0; contentPanel.add(new JLabel("Plane Model:"), gbc); gbc.gridx = 1; JTextField modelField = new JTextField(15); contentPanel.add(modelField, gbc); // Manufacturer gbc.gridx = 0; gbc.gridy = 1; contentPanel.add(new JLabel("Manufacturer:"), gbc); gbc.gridx = 1; JTextField manufacturerField = new JTextField(15); contentPanel.add(manufacturerField, gbc); // Business Seats gbc.gridx = 0; gbc.gridy = 2; contentPanel.add(new JLabel("Business Seats:"), gbc); gbc.gridx = 1; JTextField businessSeatsField = new JTextField(15); contentPanel.add(businessSeatsField, gbc); // Economy Seats gbc.gridx = 0; gbc.gridy = 3; contentPanel.add(new JLabel("Economy Seats:"), gbc); gbc.gridx = 1; JTextField economySeatsField = new JTextField(15); contentPanel.add(economySeatsField, gbc); // Add Button gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 2; gbc.anchor = GridBagConstraints.CENTER; JButton addButton = createStyledButton("Add Plane", e -> { String model = modelField.getText().trim(); String manufacturer = manufacturerField.getText().trim(); int businessSeats; int economySeats; try { businessSeats = Integer.parseInt(businessSeatsField.getText().trim()); economySeats = Integer.parseInt(economySeatsField.getText().trim()); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(frame, "Please enter valid numbers for seats!", "Error", JOptionPane.ERROR_MESSAGE); return; } if (model.isEmpty() || manufacturer.isEmpty() || businessSeats <= 0 || economySeats <= 0) { JOptionPane.showMessageDialog(frame, "Please fill out all fields correctly!", "Error", JOptionPane.ERROR_MESSAGE); return; } String query = "INSERT INTO plane (admin_id, plane_model, manufacturer, business_seats, economy_seats) VALUES (?, ?, ?, ?, ?)"; try (PreparedStatement stmt = connection.prepareStatement(query)) { stmt.setInt(1, this.adminId); stmt.setString(2, model); stmt.setString(3, manufacturer); stmt.setInt(4, businessSeats); stmt.setInt(5, economySeats); int rowsInserted = stmt.executeUpdate(); if (rowsInserted > 0) { JOptionPane.showMessageDialog(frame, "Plane added successfully!", "Success", JOptionPane.INFORMATION_MESSAGE); modelField.setText(""); manufacturerField.setText(""); businessSeatsField.setText(""); economySeatsField.setText(""); // Refresh the plane list in the "Add Flight" card refreshAddFlightPlaneList(); } else { JOptionPane.showMessageDialog(frame, "Failed to add plane.", "Error", JOptionPane.ERROR_MESSAGE); } } catch (SQLException ex) { JOptionPane.showMessageDialog(frame, "Database error: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } }); contentPanel.add(addButton, gbc); 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); return outerPanel; }take this now i want some changes in its logic see what i want the the manufacture should be taken just all capital alphabets no special charachters or numbers allowed and below its text field it should be checking realtime if doesn't follow it must show msg there and doesn't allow to create a plane until it satisfies i can explain you more now days as we see passwords has multiple critereas until they are fulfilled they doesnt allow us to create and each is shown below and got green if its done and become red until its not so i want that thing but make sure the GUI design should not affect. now same above mentioned working i want for the plane model it should be just combination of capital letters and numbers in it no special charachters allowed here
take your time and give the best code
Assistant
You're asking for real-time validation for both the Plane Model and Manufacturer fields in your Swing form, similar to how password strength is validated — with visual feedback under each field indicating whether the input meets certain criteria.
Let’s break down what you want:
✅ Requirements Recap:
-
Manufacturer Field:
- Must contain only uppercase letters (A-Z).
- No numbers or special characters allowed.
- Show real-time feedback below the field (red/green label).
-
Plane Model Field:
- Must be a combination of uppercase letters (A-Z) and digits (0-9).
- No special characters allowed.
- Real-time feedback below the field (red/green label).
-
Add Button should be disabled until all validations pass.
-
Do not disturb the overall GUI design or layout.
🛠️ Solution Overview
We'll use
DocumentListeneron eachJTextFieldto validate the input as the user types. We’ll add a smallJLabelbeneath each field that shows:- A red message if invalid
- A green message if valid
Also, we'll keep the "Add Plane" button disabled unless both fields are valid.
✅ Final Code Integration
Here's the updated version of your
createAddPlaneCard()method with the requested functionality:private JPanel createAddPlaneCard() { 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 Plane", 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; // Plane Model gbc.gridx = 0; gbc.gridy = 0; contentPanel.add(new JLabel("Plane Model:"), gbc); gbc.gridx = 1; JTextField modelField = new JTextField(15); contentPanel.add(modelField, gbc); gbc.gridx = 1; gbc.gridy = 1; gbc.anchor = GridBagConstraints.WEST; JLabel modelFeedback = new JLabel(""); modelFeedback.setFont(new Font("Arial", Font.PLAIN, 12)); contentPanel.add(modelFeedback, gbc); gbc.gridy += 1; gbc.anchor = GridBagConstraints.WEST; // Manufacturer gbc.gridx = 0; gbc.gridy = 2; contentPanel.add(new JLabel("Manufacturer:"), gbc); gbc.gridx = 1; gbc.gridy = 2; JTextField manufacturerField = new JTextField(15); contentPanel.add(manufacturerField, gbc); gbc.gridy = 3; gbc.anchor = GridBagConstraints.WEST; JLabel manufacturerFeedback = new JLabel(""); manufacturerFeedback.setFont(new Font("Arial", Font.PLAIN, 12)); contentPanel.add(manufacturerFeedback, gbc); gbc.gridy += 1; gbc.anchor = GridBagConstraints.WEST; // Business Seats gbc.gridx = 0; gbc.gridy = 4; contentPanel.add(new JLabel("Business Seats:"), gbc); gbc.gridx = 1; JTextField businessSeatsField = new JTextField(15); contentPanel.add(businessSeatsField, gbc); gbc.gridy += 1; // Economy Seats gbc.gridx = 0; gbc.gridy = 5; contentPanel.add(new JLabel("Economy Seats:"), gbc); gbc.gridx = 1; JTextField economySeatsField = new JTextField(15); contentPanel.add(economySeatsField, gbc); // Validation Flags boolean[] isValidModel = {false}; boolean[] isValidManufacturer = {false}; JButton addButton = createStyledButton("Add Plane", null); // Will set action later addButton.setEnabled(false); // Initially disabled // Add DocumentListeners modelField.getDocument().addDocumentListener(new DocumentListener() { private void update() { String text = modelField.getText(); boolean matches = text.matches("[A-Z0-9]*"); // Only uppercase letters and digits if (text.isEmpty()) { modelFeedback.setText("Please enter plane model."); modelFeedback.setForeground(Color.RED); } else if (matches) { modelFeedback.setText("Valid format."); modelFeedback.setForeground(new Color(0, 150, 0)); // Green isValidModel[0] = true; } else { modelFeedback.setText("Only uppercase letters and numbers allowed."); modelFeedback.setForeground(Color.RED); isValidModel[0] = false; } updateAddButtonState(addButton, isValidModel[0], isValidManufacturer[0]); } @Override public void insertUpdate(DocumentEvent e) { update(); } @Override public void removeUpdate(DocumentEvent e) { update(); } @Override public void changedUpdate(DocumentEvent e) {} }); manufacturerField.getDocument().addDocumentListener(new DocumentListener() { private void update() { String text = manufacturerField.getText(); boolean matches = text.matches("[A-Z]*"); if (text.isEmpty()) { manufacturerFeedback.setText("Please enter manufacturer."); manufacturerFeedback.setForeground(Color.RED); } else if (matches) { manufacturerFeedback.setText("Valid format."); manufacturerFeedback.setForeground(new Color(0, 150, 0)); isValidManufacturer[0] = true; } else { manufacturerFeedback.setText("Only uppercase letters allowed."); manufacturerFeedback.setForeground(Color.RED); isValidManufacturer[0] = false; } updateAddButtonState(addButton, isValidModel[0], isValidManufacturer[0]); } @Override public void insertUpdate(DocumentEvent e) { update(); } @Override public void removeUpdate(DocumentEvent e) { update(); } @Override public void changedUpdate(DocumentEvent e) {} }); // Set final action listener after validation setup addButton.addActionListener(e -> { String model = modelField.getText().trim(); String manufacturer = manufacturerField.getText().trim(); int businessSeats; int economySeats; try { businessSeats = Integer.parseInt(businessSeatsField.getText().trim()); economySeats = Integer.parseInt(economySeatsField.getText().trim()); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(frame, "Please enter valid numbers for seats!", "Error", JOptionPane.ERROR_MESSAGE); return; } if (model.isEmpty() || manufacturer.isEmpty() || businessSeats <= 0 || economySeats <= 0) { JOptionPane.showMessageDialog(frame, "Please fill out all fields correctly!", "Error", JOptionPane.ERROR_MESSAGE); return; } String query = "INSERT INTO plane (admin_id, plane_model, manufacturer, business_seats, economy_seats) VALUES (?, ?, ?, ?, ?)"; try (PreparedStatement stmt = connection.prepareStatement(query)) { stmt.setInt(1, this.adminId); stmt.setString(2, model); stmt.setString(3, manufacturer); stmt.setInt(4, businessSeats); stmt.setInt(5, economySeats); int rowsInserted = stmt.executeUpdate(); if (rowsInserted > 0) { JOptionPane.showMessageDialog(frame, "Plane added successfully!", "Success", JOptionPane.INFORMATION_MESSAGE); modelField.setText(""); manufacturerField.setText(""); businessSeatsField.setText(""); economySeatsField.setText(""); refreshAddFlightPlaneList(); } else { JOptionPane.showMessageDialog(frame, "Failed to add plane.", "Error", JOptionPane.ERROR_MESSAGE); } } catch (SQLException ex) { JOptionPane.showMessageDialog(frame, "Database error: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } }); // Add Button gbc.gridx = 0; gbc.gridy = 6; gbc.gridwidth = 2; gbc.anchor = GridBagConstraints.CENTER; contentPanel.add(addButton, gbc); 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); return outerPanel; }
🧰 Helper Method (Optional)
Add this helper method to avoid repeating code:
private void updateAddButtonState(JButton button, boolean... conditions) { for (boolean cond : conditions) { if (!cond) { button.setEnabled(false); return; } } button.setEnabled(true); }
✨ Summary
- ✅ Real-time validation using
DocumentListener. - ✅ Visual feedback via colored labels below fields.
- ✅ Button is only enabled when both fields are valid.
- ✅ Input constraints:
- Plane Model:
[A-Z0-9]* - Manufacturer:
[A-Z]*
- Plane Model:
- ✅ Clean integration without disturbing existing layout or design.
Let me know if you'd like to add more validation rules or extend it to other fields!
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.