private JPanel createAddPlaneCard() { JPanel outerPanel = new JPanel(new…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
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
To implement the real-time validation for the manufacturer and plane model fields, we can use DocumentListener to monitor changes in the text fields. We'll provide immediate feedback to the user if the input does not meet the criteria. We'll use a JLabel below each text field to display the validation status.
Here's the updated code with the required changes:
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.text.Document;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
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);
JLabel modelValidationLabel = new JLabel("");
modelValidationLabel.setForeground(Color.RED);
gbc.gridy = 1;
contentPanel.add(modelValidationLabel, gbc);
modelField.getDocument().addDocumentListener(new ModelValidationListener(modelValidationLabel));
// Manufacturer
gbc.gridx = 0;
gbc.gridy = 2;
contentPanel.add(new JLabel("Manufacturer:"), gbc);
gbc.gridx = 1;
JTextField manufacturerField = new JTextField(15);
contentPanel.add(manufacturerField, gbc);
JLabel manufacturerValidationLabel = new JLabel("");
manufacturerValidationLabel.setForeground(Color.RED);
gbc.gridy = 3;
contentPanel.add(manufacturerValidationLabel, gbc);
manufacturerField.getDocument().addDocumentListener(new ManufacturerValidationListener(manufacturerValidationLabel));
// 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);
// 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);
// Add Button
gbc.gridx = 0;
gbc.gridy = 6;
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;
}
private class ModelValidationListener implements DocumentListener {
private JLabel label;
public ModelValidationListener(JLabel label) {
this.label = label;
}
@Override
public void insertUpdate(DocumentEvent e) {
validateModel();
}
@Override
public void removeUpdate(DocumentEvent e) {
validateModel();
}
@Override
public void changedUpdate(DocumentEvent e) {
validateModel();
}
private void validateModel() {
Document document = e.getDocument();
String text = null;
try {
text = document.getText(0, document.getLength());
} catch (BadLocationException ex) {
ex.printStackTrace();
}
if (text != null && text.matches("^[A-Z0-9]+$")) {
label.setText("Valid");
label.setForeground(Color.GREEN);
} else {
label.setText("Invalid: Only capital letters and numbers allowed");
label.setForeground(Color.RED);
}
}
}
private class ManufacturerValidationListener implements DocumentListener {
private JLabel label;
public ManufacturerValidationListener(JLabel label) {
this.label = label;
}
@Override
public void insertUpdate(DocumentEvent e) {
validateManufacturer();
}
@Override
public void removeUpdate(DocumentEvent e) {
validateManufacturer();
}
@Override
public void changedUpdate(DocumentEvent e) {
validateManufacturer();
}
private void validateManufacturer() {
Document document = e.getDocument();
String text = null;
try {
text = document.getText(0, document.getLength());
} catch (BadLocationException ex) {
ex.printStackTrace();
}
if (text != null && text.matches("^[A-Z]+$")) {
label.setText("Valid");
label.setForeground(Color.GREEN);
} else {
label.setText("Invalid: Only capital letters allowed");
label.setForeground(Color.RED);
}
}
}
JLabel components below the JTextFields to display validation messages.