2017-10-19 57 views
-1

J'ai un programme d'interface graphique multi-images et l'un des problèmes est que lorsque j'ajoute un objet à ArrayList dans Class1, il ne reflète pas dans le JComboBox de Class2.ArrayList dans une classe est mis à jour en utilisant GUI, mais ne change pas dans le JComboBox dans un autre

Un peu d'histoire:

J'ai un NewDepartmentFrame qui ajoute les ministères à une classe de SystemData. La classe SystemData contient un accesseur appelé 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()); 
     } 
    } 
} 
+0

Je ne pouvais pas trouver où le ministère est ajouté (un peu trop/(code erroné) ici?), Mais 'getAllDepartments' est le clonage de la liste - ajouter à l'original liste (ou un clone) ne sera pas ajouté au (autre) clone (s)! –

+0

@CarlosHeuberger Oui, mon code est confus même pour moi. J'espère que ça va mieux avec la pratique. Mon département est ajouté à ma classe 'NewDepartmentFrame'. Permettez-moi de l'ajouter à ma question, alors vous le verrez. – angelaM

Répondre

0

Qu'est-ce que vous avez besoin est d'utiliser le DefaultComboBoxModel comme modèle de données pour la zone de liste déroulante. Swing est basé sur un modèle appelé MVC:

  • M pour le modèle, d'où le DefaultComboBoxModel
  • V pour la vue, votre JComboBox
  • C pour le contrôleur, relie la vue et le modèle

Utilisez le DefaultComboBoxModel pour stocker le departments et la méthode addDepartment ajoutera un nouveau département à ce modèle en appelant addElement. Chaque fois que vous ajoutez un élément à ce modèle, la zone de liste déroulante sera mise à jour automatiquement.

Vous pouvez trouver un exemple here