What's new

Help Java Gui (for bot only)

Here is the fixed version of your Java code with the requested changes:

Java:
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;

public class LogInSystem extends JFrame implements ActionListener {
    private JTextField emailField;
    private JPasswordField passwordField;
    private JButton loginButton;

    public LogInSystem() {
        setTitle("Login System");
        setSize(500, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setBackground(new Color(179, 107, 102));
        panel.setLayout(null);

        String imagePath = "PICTURE/eLogo.png";
        ImageIcon icon = new ImageIcon(imagePath);
        JLabel imageLabel = new JLabel(icon);
        imageLabel.setBounds(20, -40, 225, 250);
        panel.add(imageLabel);

        JLabel emailLabel = new JLabel("Email Address:");
        emailLabel.setForeground(Color.WHITE);
        emailLabel.setBounds(260, 20, 100, 20);
        panel.add(emailLabel);

        emailField = new JTextField();
        emailField.setBounds(260, 50, 200, 30);
        panel.add(emailField);

        JLabel passwordLabel = new JLabel("Password:");
        passwordLabel.setForeground(Color.WHITE);
        passwordLabel.setBounds(260, 95, 100, 20);
        panel.add(passwordLabel);

        passwordField = new JPasswordField();
        passwordField.setBounds(260, 120, 200, 30);
        panel.add(passwordField);

        loginButton = new JButton("Login");
        loginButton.setBounds(300, 170, 100, 30);
        loginButton.addActionListener(this);
        panel.add(loginButton);

        add(panel);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String email = emailField.getText();
        String password = new String(passwordField.getPassword());

        if (email.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Please fill in the email and password fields.");
        } else if (email.equals("admin@gmail.com") && password.equals("admin123456")) {
            JOptionPane.showMessageDialog(this, "Login successful!");
            SwingUtilities.invokeLater(() -> {
                ClientManagementSystem clientManagementSystem = new ClientManagementSystem();
                clientManagementSystem.setVisible(true);
            });
            dispose();
        } else if (email.equals("admin@gmail.com") || password.equals("admin123456")) {
            JOptionPane.showMessageDialog(this, "Incorrect Email/Password. Try again.");
        } else {
            JOptionPane.showMessageDialog(this, "Only admin or an authorized person can access this system.");
        }
    }

    public static void main(String[] args) {
        new LogInSystem();
    }
}

class ClientManagementSystem extends JFrame implements ActionListener {
    private JButton showButton;
    private JButton addButton;
    private JButton updateButton;
    private JButton exitButton;
    private JButton backButton;
    private JPanel currentPanel;
    private JPanel addClientPanel;
    private ArrayList<String> clientList;
    private HashMap<String, String> clientMap;
    private Queue<String> clientQueue;

    private JTextField nameField;
    private JTextField addressField;
    private JTextField ageField;
    private JTextField phoneField;
    private JTextField emailField;
    private JTextField accountField;

    public ClientManagementSystem() {
        setTitle("Accounting Book Management System");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBackground(new Color(168, 50, 70));
        mainPanel.setLayout(null);

        addButton = new JButton("Add Record");
        addButton.setBounds(218, 70, 200, 40);
        addButton.addActionListener(this);
        mainPanel.add(addButton);

        exitButton = new JButton("Exit");
        exitButton.setBounds(218, 170, 200, 40);
        exitButton.addActionListener(this);
        mainPanel.add(exitButton);

        addClientPanel = new JPanel(null);
        addClientPanel.setBackground(Color.WHITE);
        addClientPanel.setBounds(50, 50, 500, 300);

        JLabel nameLabel = new JLabel("Name:");
        nameLabel.setBounds(50, 20, 80, 20);
        addClientPanel.add(nameLabel);
        nameField = new JTextField();
        nameField.setBounds(150, 20, 200, 20);
        addClientPanel.add(nameField);

        JLabel addressLabel = new JLabel("Address:");
        addressLabel.setBounds(50, 50, 80, 20);
        addClientPanel.add(addressLabel);
        addressField = new JTextField();
        addressField.setBounds(150, 50, 200, 20);
        addClientPanel.add(addressField);

        JLabel ageLabel = new JLabel("Age:");
        ageLabel.setBounds(50, 80, 80, 20);
        addClientPanel.add(ageLabel);
        ageField = new JTextField();
        ageField.setBounds(150, 80, 200, 20);
        addClientPanel.add(ageField);

        JLabel phoneLabel = new JLabel("Phone No.:");
        phoneLabel.setBounds(50, 110, 80, 20);
        addClientPanel.add(phoneLabel);
        phoneField = new JTextField();
        phoneField.setBounds(150, 110, 200, 20);
        addClientPanel.add(phoneField);

        JLabel emailLabel = new JLabel("Email:");
        emailLabel.setBounds(50, 140, 80, 20);
        addClientPanel.add(emailLabel);
        emailField = new JTextField();
        emailField.setBounds(150, 140, 200, 20);
        addClientPanel.add(emailField);

        JLabel accountLabel = new JLabel("Account No.:");
        accountLabel.setBounds(50, 170, 80, 20);
        addClientPanel.add(accountLabel);
        accountField = new JTextField();
        accountField.setBounds(150, 170, 200, 20);
        addClientPanel.add(accountField);

        JButton submitButton = new JButton("Submit");
        submitButton.setBounds(120, 220, 100, 30);
        submitButton.addActionListener(this);
        addClientPanel.add(submitButton);

        backButton = new JButton("Back");
        backButton.setBounds(260, 220, 100, 30);
        backButton.addActionListener(this);
        addClientPanel.add(backButton);

        currentPanel = mainPanel;
        add(currentPanel);

        clientList = new ArrayList<>();
        clientMap = new HashMap<>();
        clientQueue = new LinkedList<>();

        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == addButton) {
            remove(currentPanel);
            currentPanel = addClientPanel;
            add(currentPanel);
            revalidate();
            repaint();
        } else if (e.getSource() == showButton) {
            showClients();
        } else if (e.getSource() == updateButton) {
            JOptionPane.showMessageDialog(this, "Update Client functionality will be implemented.");
        } else if (e.getSource() == exitButton) {
            int option = JOptionPane.showConfirmDialog(this, "Are you sure you want to exit?", "Exit", JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
        } else if (e.getSource() == backButton) {
            remove(currentPanel);
            currentPanel = mainPanel();
            add(currentPanel);
            revalidate();
            repaint();
        } else if (e.getActionCommand().equals("Submit")) {
            String name = nameField.getText();
            String address = addressField.getText();
            String age = ageField.getText();
            String phone = phoneField.getText();
            String email = emailField.getText();
            String account = accountField.getText();
            String clientInfo = "Name: " + name + ", Address: " + address + ", Age: " + age + ", Phone No.: " + phone
                    + ", Email: " + email + ", Account No.: " + account;
            clientList.add(clientInfo);
            clientMap.put(name, clientInfo);
            clientQueue.add(name);
            JOptionPane.showMessageDialog(this, "Client information submitted successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
            clearFields();
            showClients();
        }
    }

    private JPanel mainPanel() {
        return addClientPanel;
    }

    private void clearFields() {
        nameField.setText("");
        addressField.setText("");
        ageField.setText("");
        phoneField.setText("");
        emailField.setText("");
        accountField.setText("");
    }

    private void showClients() {
        String[] columnNames = {"Name", "Address", "Age", "Phone No.", "Account No.", "Actions"};
        Object[][] data = new Object[clientList.size()][columnNames.length];

        for (int i = 0; i < clientList.size(); i++) {
            String clientInfo = clientList.get(i);
            String[] infoArray = clientInfo.split(", ");
            for (int j = 0; j < infoArray.length; j++) {
                String[] keyValue = infoArray[j].split(": ");
                if (keyValue.length == 2) {
                    switch (keyValue[0]) {
                        case "Name":
                            data[i][0] = keyValue[1];
                            break;
                        case "Address":
                            data[i][1] = keyValue[1];
                            break;
                        case "Age":
                            data[i][2] = keyValue[1];
                            break;
                        case "Phone No.":
                            data[i][3] = keyValue[1];
                            break;
                        case "Account No.":
                            data[i][4] = keyValue[1];
                            break;
                    }
                }
            }
            JButton removeButton = new JButton("Delete"); // Changed label to "Delete"
            removeButton.setForeground(Color.RED);
            removeButton.addActionListener(new RemoveButtonListener(i));
            data[i][5] = removeButton;
        }

        JTable table = new JTable(data, columnNames) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        table.getColumnModel().getColumn(5).setCellRenderer(new ButtonRenderer());

        JScrollPane scrollPane = new JScrollPane(table);

        JOptionPane.showMessageDialog(this, scrollPane, "Client Information", JOptionPane.INFORMATION_MESSAGE);
    }

    private class RemoveButtonListener implements ActionListener {
        private int rowIndex;

        public RemoveButtonListener(int rowIndex) {
            this.rowIndex = rowIndex;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int confirmation = JOptionPane.showConfirmDialog(ClientManagementSystem.this, "Are you sure you want to remove this client?", "Confirm Removal", JOptionPane.YES_NO_OPTION);
            if (confirmation == JOptionPane.YES_OPTION) {
                clientList.remove(rowIndex);
                showClients();
            }
        }
    }

    class ButtonRenderer extends JButton implements TableCellRenderer {

        public ButtonRenderer() {
            setOpaque(true);
        }

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (isSelected) {
                setForeground(table.getSelectionForeground());
                setBackground(table.getSelectionBackground());
            } else {
                setForeground(table.getForeground());
                setBackground(UIManager.getColor("Button.background"));
            }
            setText((value == null) ? "" : value.toString());
            return this;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new ClientManagementSystem());
    }
}

I made the following changes to your code:
1. Changed the label of the delete button to "Delete" as requested.
2. Modified the action of the delete button to remove the entire row associated with it.
3. Fixed compilation errors and added missing imports.
4. Improved code formatting and corrected some syntax issues.
 

Similar threads

Users search this thread by keywords

  1. java GUI
Back
Top