简体   繁体   中英

How to change a value in the Jtextfield or JComboBox using the data structure linkedlist?

I already used the set method in a LinkedList to change a value but it is not working for me. Everytime it will print the values in the console, it results to the same values. It doesn't change at all. Can anyone help me? Thank you!

Here's my code:

btnUpdate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for(int i = 0; i < emp.id.size(); i++){
                if(emp.id.get(i).equals(textField_6.getText())){
                    Employee emp = new Employee();

                    emp.setFirstname(textField_1.getText());
                    emp.setLastname(textField_2.getText());
                    emp.setAge(textField_3.getText());
                    emp.setGender(comboBox.getSelectedItem());
                    emp.setDesignation(comboBox_4.getSelectedItem());


                    emp.fName.set(i, emp.getFirstname());
                    emp.LName.set(i, emp.getLastname());
                    emp.Age.set(i, emp.getAge());
                    emp.Gender.set(i, emp.getGender());
                    emp.Designation.set(i, emp.getDesignation());
                }
            }

            JOptionPane.showMessageDialog(null, "Successfully changed!");

            System.out.println(emp.id);
            System.out.println(emp.fName);
            System.out.println(emp.LName);
            System.out.println(emp.Age);
            System.out.println(emp.Gender);
            System.out.println(emp.Designation);

            String cmd = e.getActionCommand();

            if(cmd.equals("Update")){
                dispose();
                exit();
            }
        }
    });
Employee emp = new Employee();

You are not updating an existing object, you are creating a new object and you never all the object to the LinkedList.

if(emp.id.get(i).equals(textField_6.getText())){

You have the if condition to find the "emp" object you want to update, so use that object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM