简体   繁体   中英

Java SWT ModifyListener set a Boolean variable

i am writing a small Text-Editor with Java SWT. If the textfield of the object "Text" are modified, the Listener have to set a boolean value to true, that the textfield was changed. And when i want to quit the editor it should ask me to save if the textfield was changed. But the boolean variable changes their value only in their ModifyListener? If i push on the quit button, the selectionlistener reacts and it should ask me to save, but it didn't ask to.

What is my failure?

public class ModifyListenerTextField implements ModifyListener {

 private Boolean changedTextField;

 public ModifyListenerTextField(Boolean changedTextField){
     this.changedTextField = changedTextField;
 }

 @Override
 public void modifyText(ModifyEvent arg0) {
     if(!changedTextField.booleanValue()){
         changedTextField = Boolean.TRUE;
     }
 }

}

It is quite simple. You dont need to implement anything. You may alter the below code upon your will

JTextField field = new JTextField();

field.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                booleanVar = true;
            }
        });

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