简体   繁体   English

无法让 documentlistener 工作启用或禁用 Java(Netbeans)中的按钮,我做错了什么?

[英]Unable to get documentlistener to work enable or disable a button in Java (Netbeans), what am I doing wrong?

I'm trying to enable or disable a button based on whether a text area is empty and for further context, I'm doing this in Netbeans.我正在尝试根据文本区域是否为空来启用或禁用按钮,为了进一步了解,我在 Netbeans 中执行此操作。 The button disabled by default.该按钮默认禁用。

Based on my research so far, what I need to detect the change in the text area is a document listener... No matter how I add the code that I have managed to figure out an plagiarize, it either has errors in it or does nothing... messagearea is the name of my textarea and sendb is the button.根据我目前的研究,我需要检测文本区域的变化是一个文档监听器......无论我如何添加我设法找出抄袭的代码,它要么有错误,要么确实没什么... messagearea 是我的 textarea 的名称,而 sendb 是按钮。 This is the best I have so far:这是我迄今为止最好的:

public NewJFrame (DocumentListener Frame){
    messagearea.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            //throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
            if (messagearea.getText().equals("")){
                sendb.setEnabled(false);
                System.out.println("false");
            } else
            {
                sendb.setEnabled(true);
                System.out.println("true");
            }
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            //throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
            if (messagearea.getText().equals("")){
                sendb.setEnabled(false);
                System.out.println("false");
            } else
            {
                sendb.setEnabled(true);
                System.out.println("true");
            }
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            //throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
            if (messagearea.getText().equals("")){
                sendb.setEnabled(false);
                System.out.println("false");
            } else
            {
                sendb.setEnabled(true);
                System.out.println("true");
            }
        }
    });
}

I've put this after the variable declaration.我把它放在变量声明之后。 It doesn't even print any output, so I now I've made a mistake in making the listener actually "listen", lol Any help would be appreciated.它甚至不打印任何 output,所以我现在犯了一个错误,让听众真正“听”,大声笑任何帮助将不胜感激。

I solved this problem.我解决了这个问题。

In the Design tab of the main class with the textarea in it I right clicked on textarea, selected properties, went to the code tab, clicked the ellipses on the Post-Listeners Code category and pasted the above code in the provided field.在主 class 的设计选项卡中,其中包含 textarea,我右键单击 textarea,选择属性,转到代码选项卡,单击 Post-Listeners Code 类别上的省略号,并将上述代码粘贴到提供的字段中。 The button now behaves as I wanted.现在按钮的行为如我所愿。

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

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