一个 class 中的 ArrayList 使用 GUI 更新,但在另一个中的 JComboBox 中没有更改

ArrayList in one class is updated using GUI but is not changing in the JComboBox in another

我有一个多框架的 GUI 程序,其中一个问题是当我在 Class1 中的 ArrayList 中添加一个对象时,它没有反映在 Class2 的 JComboBox 中。

一些背景:

我有一个 NewDepartmentFrame 可以将部门添加到 SystemData class。 SystemData class 包含一个名为 getAllDepartments()

的访问器
import java.util.*;
import java.io.*;

public class SystemData {
    private static ArrayList<Department> departments = new ArrayList<>();

    public static ArrayList<Department> getAllDepartments() {
        return (ArrayList<Department>) departments.clone();
    }
    //there is more to this class, but I don't think it is relevant for the
    //question at the moment
}
//here is part of another class called NewDepartmentFrame that adds the 
//department

public class NewDepartmentFrame extends JFrame{

public Department saveDepartment (){
   Department temp = new Department(dName, dLocation);
   SystemData.addDepartment(temp);
   return temp;
}

public class Listeners implements ActionListener{
   @Override
   public void actionPerformed(ActionEvent e){
       if(e.getSource() == bSave){
           dName = tAdd.getText();
           tAdd.setText("");
           main.add(bSave);
           JOptionPane.showMessageDialog(rootPane, "You have entered a new department");
           saveDepartment();
}

//I know that my code is quite messy but I'm still new to Java
public class EmployeeFrame extends JFrame {
    private static JFrame frame = new JFrame();
    private JLabel lfName = new JLabel("First Name ");
    private JLabel llName = new JLabel("Last Name ");
    private JLabel lAddress = new JLabel("Address ");
    private JLabel lEmployee = new JLabel("Enter Employee ID");
    private JLabel lPLevel = new JLabel("Pay Level ");
    private JLabel lDepartment = new JLabel("Department");

    private JTextField fName = new JTextField();
    private JTextField lName = new JTextField();
    private JTextField address = new JTextField();
    private JTextField employee = new JTextField();

    private JRadioButton b1 = new JRadioButton("Male");
    private JRadioButton b2 = new JRadioButton("Female");
    private ButtonGroup bGroup = new ButtonGroup();

    private JComboBox pLevel = new JComboBox();
    private JComboBox department = new JComboBox();

    private JButton bAlter = new JButton("Alter");
    private JButton bDelete = new JButton("Delete");
    private JButton bAdd = new JButton("Add");
    private JButton bClear = new JButton("Clear");
    private JButton bSave = new JButton("Save");
    private JButton bClose = new JButton("Close");

    private String fNameInput;
    private String lNameInput;
    private int payLevel;
    private boolean alter = false;
    private String input;
    private int empID = Employee.getId();

    private Employee e = new Employee();
    private Employee temp;
    private Department d = new Department();
    private JPanel main = new JPanel();
    private TheListeners l = new TheListeners();

    public EmployeeFrame() {
        frame.setTitle("Manage Employees");
        frame.setLocationRelativeTo(null);
        frame.setSize(500, 300);
        frame.add(main);

        for (double d : e.getPayList()) {
            pLevel.addItem(d);
        }

        department.setModel(new DefaultComboBoxModel(SystemData.getAllDepartments().toArray()));
        //why does it not reflect the new Department added to the ArrayList

        main.add(lfName);
        fName.setColumns(ALLBITS);
        main.add(fName);
        main.add(llName);
        lName.setColumns(ALLBITS);
        main.add(lName);

        main.add(b1);
        main.add(b2);
        bGroup.add(b1);
        bGroup.add(b2);

        main.add(lAddress);
        address.setColumns(ALLBITS);
        main.add(address);

        main.add(lPLevel);
        main.add(pLevel);

        main.add(lDepartment);

        main.add(department);

        main.add(lEmployee);
        employee.setColumns(ALLBITS);
        main.add(employee);

        bSave.setEnabled(false);
        main.add(bAlter);
        main.add(bDelete);
        main.add(bAdd);
        main.add(bClear);
        main.add(bSave);
        main.add(bClose);

        //add listeners
        b1.addItemListener(l);
        b2.addItemListener(l);

        bAlter.addActionListener(l);
        bDelete.addActionListener(l);
        bAdd.addActionListener(l);
        bClear.addActionListener(l);
        bSave.addActionListener(l);
        bClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });
        //main.setVisible(true);
        frame.setVisible(true);
    }

    public class TheListeners implements ActionListener, ItemListener {
        private SystemData s = new SystemData();

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == bAdd) {
                bSave.setEnabled(true);
                String input1 = fName.getText();
                String input2 = lName.getText();
                String input3 = address.getText();
                int input4 = (Integer) pLevel.getSelectedIndex();
                input4++; //add one so that it returns the correct paylevel
                System.out.println("test1");
                boolean gender;
                Department d = (Department) department.getSelectedItem();
                gender = b1.isSelected();

                temp = new Employee(input1, input2, gender, input3, input4, d);
                System.out.println(temp.toString());
            } else if (e.getSource() == bClear) {
                bSave.setEnabled(false);
                fName.setText("");
                lName.setText("");
                address.setText("");
                pLevel.setSelectedItem(null);
                department.setSelectedItem(null);
                employee.setText("");
                System.out.println("test2");

            } else if (e.getSource() == bAlter) {
                String input = employee.getText();
                int empID = Integer.parseInt(input);
                System.out.println("test3");

                temp = SystemData.getEmployee(empID);
                if (temp != null) {
                    fName.setText(temp.getFirstName());
                    lName.setText(temp.getLastName());
                    b1.setSelected(temp.isGender());
                    b2.setSelected(temp.isGender());
                    address.setText(temp.getAddress());
                    pLevel.setSelectedIndex(temp.getPayLevel() - 1);
                    department.setSelectedItem(temp.getCurDepartment());

                    alter = true;
                    System.out.println("test4");
                }
            } else if (e.getSource() == bDelete) {
                System.out.println("test5");
                String input = employee.getText();
                int empID = Integer.parseInt(input);
                System.out.println("test6");

                 /*temp = SystemData.getEmployee(empID);

                 if(!(empID < 10000 || empID > 29999)){
                     if(temp != null){
                        fName.setText(temp.getFirstName());
                        lName.setText(temp.getLastName());
                        b1.setSelected(temp.isGender());
                        b2.setSelected(temp.isGender());
                        address.setText(temp.getAddress());
                        pLevel.setSelectedIndex(temp.getPayLevel() - 1);
                        department.setSelectedItem(temp.getCurDepartment());

                        SystemData.deleteEmployee(empID);

                        JOptionPane.showMessageDialog(null, "You have deleted "+ temp.toString() + "from the database");

                 }
                 else{

                 JOptionPane.showMessageDialog(null, "Please enter a valid ID");

                 }
                */

            } else if (e.getSource() == bSave) {
                if (alter) {
                    bSave.setEnabled(true);
                    SystemData.getEmployee(empID).setFirstName(fName.getText());
                    SystemData.getEmployee(empID).setLastName(lName.getText());
                    SystemData.getEmployee(empID).setAddress(address.getText());
                    SystemData.getEmployee(empID).setPayLevel(pLevel.getSelectedIndex() + 1);
                    SystemData.getEmployee(empID).setCurDepartment((Department) department.getSelectedItem());
                    SystemData.getEmployee(empID).setGender(b1.isSelected());
                    bSave.setEnabled(false);
                } else {
                    //bSave.setEnabled(false);
                    fName.setText("");
                    lName.setText("");
                    address.setText("");
                    pLevel.setSelectedItem(null);
                    department.setSelectedItem(null);
                    employee.setText("");

                    System.out.println(empID);
                    SystemData.setEmployees(empID, temp);
                    //d.setEmployees(temp);
                    empID++;
                    //SystemData.employees.put(empID, temp);
                    //d.setEmployees(temp);
                    int print = empID - 1;
                    JOptionPane.showMessageDialog(null, "You've added a new employee. \n ID number:" + print);
                }
            }
        }

        @Override
        public void itemStateChanged(ItemEvent e) {
            //System.out.println(e.getItem().toString());
        }
    }
}

您需要使用 DefaultComboBoxModel 作为组合框的数据模型。 Swing 基于称为 MVC 的模式:

  • M 代表型号,因此 DefaultComboBoxModel
  • V 表示视图,您的 JComboBox
  • C 用于控制器,链接视图和模型

使用 DefaultComboBoxModel 存储 departments 方法 addDepartment 将通过调用 addElement 向该模型添加一个新部门。每次向该模型添加元素时,组合框都会自动更新。

你可以找一个例子here