简体   繁体   English

如何使用数据结构链表更改Jtextfield或JComboBox中的值?

[英]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. 我已经在LinkedList中使用set方法来更改值,但是它对我不起作用。 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. 您不是在更新现有对象,而是在创建新对象,并且永远不会将所有对象都发送到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. 您具有if条件,可以找到要更新的“ emp”对象,因此请使用该对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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