无法启用灰色(.setEnabled(false))jtextfield或jtextarea
不幸的是,我无法打开JTEXTFIELD或JTEXTFIELD(两者都尝试)的.setenable()。它一直保持灰色,因此用户无法输入。请帮助我。
详细信息:Tatwo可以是JTEXTFIELD或JTEXTAREA,但我尝试的任何尝试都无法启用。应该禁用A,但应启用B的B,因此,如果用户选择A/她无法在Tatwo字段中输入值,但是如果用户选择B,则可以在Tatwo中写入。
该方法如下:
public void btnAddtreeAction() {
this.btnAddtree.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel dialogPanel = new JPanel();
dialogPanel.setPreferredSize(new Dimension(60,60));
String[] choices = {"A", "B"};
JComboBox<String> cb = new JComboBox<String>(choices);
JTextArea taOne = new JTextArea(1,30);
JTextField taTwo = new JTextField();
taTwo.setEnabled(false);
Object[] myObject = {"Options:", cb,
"Input first:", taOne,
"Input second:", taTwo};
JOptionPane.showConfirmDialog(frame, myObject, "Form", JOptionPane.OK_CANCEL_OPTION);
if (cb.getSelectedItem().toString().equals("A")) {
//something will happen here
} else if (cb.getSelectedItem().toString().equals("B")) {
taTwo.setEnabled(true);
//something will happen here
}
}
});
}
Unfortunately, I cannot turn on .setEnable() for a JTextField, or a JTextField (tried both). It keeps remaining gray, so users cannot type. Please help me out.
Details: taTwo can be either a JTextField or JTextArea but any I try cannot be enabled. It should be disabled for A but should be enabled for B, so if user choose A he/she can NOT enter a value in taTwo field, but if the user choose B he/she can write in taTwo.
The method is the following:
public void btnAddtreeAction() {
this.btnAddtree.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel dialogPanel = new JPanel();
dialogPanel.setPreferredSize(new Dimension(60,60));
String[] choices = {"A", "B"};
JComboBox<String> cb = new JComboBox<String>(choices);
JTextArea taOne = new JTextArea(1,30);
JTextField taTwo = new JTextField();
taTwo.setEnabled(false);
Object[] myObject = {"Options:", cb,
"Input first:", taOne,
"Input second:", taTwo};
JOptionPane.showConfirmDialog(frame, myObject, "Form", JOptionPane.OK_CANCEL_OPTION);
if (cb.getSelectedItem().toString().equals("A")) {
//something will happen here
} else if (cb.getSelectedItem().toString().equals("B")) {
taTwo.setEnabled(true);
//something will happen here
}
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的
ActionPerformed
方法中,您正在创建 newJTEXTFIELD
您未添加到GUI中。我认为您有另一个名为tatwo
的变量,您未发布的代码中的某些变量。您是 在ActionPerformed
方法中更改该变量。尝试删除您的代码的这一行:In your
actionPerformed
method, you are creating a newJTextField
which you are not adding to your GUI. I presume that you have another variable namedtaTwo
somehere in the code that you did not post. You are not changing that variable inside theactionPerformed
method. Try removing this line of your code:如果我正确理解您的问题,则需要将enableMet/禁用代码移动到另一个
actionListener
中,然后将其添加到ComboBox中。这样的事情:If I correctly understand your question, you need to move your enablemet/disablement code into another
ActionListener
and add it to your combobox. Something like this: