保持jButton为灰色,直到jTextFields!= null [英] keep jButton grey until jTextFields != null
问题描述
我有4个jTextField,一旦按下提交按钮,我会将输入保存到文件中.我希望能够保持禁用提交按钮,直到每个字段至少不为null为止.我在哪里可以放这样的东西
I have 4 jTextFields that I save the input to a file once a submit button is pressed. I want to be able to keep the submit button disabled until each field is at least not null. Where can i put something like this
if(jTextField1 == null || jTextField2 == null || jTextField3 == null || jTextField4 == null){
jButton2.setEnabled(false);
}
,以便程序将启用/禁用实时按钮.就像最后一个字段中甚至有1个字符一样,我是否希望启用它?
so that the program will enable/disable the button live. Like once the last field even has 1 character in it I want it to be enabled?
推荐答案
您需要添加侦听器以检测用户何时输入文本.为了让它注册任何更改(而不仅仅是在用户按下 Enter
时),您应该将 DocumentListener
附加到每个 JTextField
.
You need to add listeners to detect when the user enters text. In order to have it register any change (and not just when the user hits Enter
) you should attach a DocumentListener
to the underlying document of each JTextField
.
然后,让每个侦听器调用一个函数来进行检查并相应地更新 JButton
的 enabled
状态.
Then, have each listener call a function to do your check and update the JButton
's enabled
status accordingly.
这篇关于保持jButton为灰色,直到jTextFields!= null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!