-1
我有一個多幀GUI程序,其中一個問題是,當我將一個對象添加到Class1中的ArrayList時,它不反映在Class2的JComboBox中。使用GUI更新一個類中的ArrayList,但不會在另一個JComboBox中更改
一些背景資料:
我有一個NewDepartmentFrame,增加了部門的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());
}
}
}
我找不到其中所添加的部門(有點過分/(錯誤)代碼在這裏?),但'getAllDepartments'被克隆列表 - 添加到原列表(或克隆)不會添加到(其他)克隆中! –
@CarlosHeuberger是的,我的代碼甚至對我來說都很混亂。我希望能夠通過練習變得更好。我的部門實際上是添加在我的'NewDepartmentFrame'類中。讓我把它添加到我的問題,然後你會看到它。 – angelaM