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

时间:2017-10-19 19:42:48

标签: java user-interface arraylist jcombobox

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

一些背景知识:

我有一个NewDepartmentFrame,它将Departments添加到SystemData类中。 SystemData类包含一个名为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());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

  • M代表模型,因此DefaultComboBoxModel
  • V for View,您的JComboBox
  • C for controller,链接视图和模型

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

您可以找到示例here