简体   繁体   English

Java SWT ModifyListener设置一个布尔变量

[英]Java SWT ModifyListener set a Boolean variable

i am writing a small Text-Editor with Java SWT. 我正在用Java SWT编写一个小型Text-Editor。 If the textfield of the object "Text" are modified, the Listener have to set a boolean value to true, that the textfield was changed. 如果修改了对象“文本”的文本字段,则侦听器必须将布尔值设置为true,即该文本字段已更改。 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? 但是布尔变量仅在其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;
            }
        });

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

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