简体   繁体   English

如何在Java中禁用ButtonGroup中的JRadioButton

[英]How to disable a JRadioButton inside a ButtonGroup in Java

i have four JRatioButtons inside a ButtonGroup in Java. 我在Java中的ButtonGroup中有四个JRatioButtons。 The two first are enabled and the other two are disabled. 首先启用两个,禁用其他两个。 If one specific JRatioButton is selected i need to enable the two disabled JRatioButtons. 如果选择了一个特定的JRatioButton,我需要启用两个禁用的JRatioButton。

Im trying this to find the state of the buttons and enable the disabled ones, apparently i found the ones with the disable state but doesnt change that state. 我试图找到按钮的状态并启用禁用状态,显然我发现那些具有禁用状态但没有改变该状态。

private void activateButtons() {
    Enumeration<AbstractButton> elements = myButtonGroup.getElements();
    while (elements.hasMoreElements()) {
          AbstractButton button = (AbstractButton)elements.nextElement();
          if (button.isEnabled()) {
            System.out.println("This button is disabled! The text of the button is: '" + button.getText() + "'");
            button.setEnabled(true);
          }
    }
}

Im getting the text of the disabled buttons, but i cant disable them. 我得到了禁用按钮的文本,但我无法禁用它们。

Any help? 有帮助吗? Thanks! 谢谢!

I don't know if you any problems in finding the reference of the radio buttons in the second group or you just cannot disable the radio buttons. 我不知道在第二组中找到单选按钮的引用是否有任何问题,或者您无法禁用单选按钮。

For the first question, it is simple, you just keep the reference of the radio buttons in the second group. 对于第一个问题,很简单,您只需在第二组中保留单选按钮的引用即可。

For the second question, you need to subclass a JRadioButton because I found you can not simply call disable for an object of radio button. 对于第二个问题,您需要子类化JRadioButton,因为我发现您不能简单地为单选按钮对象调用disable。

The code sample of the sub class would be like this. 子类的代码示例是这样的。

this.editable = editable;
if (editable) {
    this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    super.enableEvents(Event.MOUSE_DOWN | Event.MOUSE_UP);
} else {
    this.setCursor(CursorFactory.createUnavailableCursor());
    super.disableEvents(Event.MOUSE_DOWN | Event.MOUSE_UP);
}

Try this, it works. 试试这个,它有效。

AbstractButton button = ...

button.getModel().setEnabled(true/false)

Ya, you setEnabled(true) to an enabled RadioButton. Ya,你将setEnabled(true)设置为启用的RadioButton。
So here the edited, hope can help someone. 所以在这里编辑,希望可以帮助某人。

private void activateButtons() 
{
    Enumeration<AbstractButton> elements = myButtonGroup.getElements();
    while (elements.hasMoreElements()) 
    {
          AbstractButton button = (AbstractButton)elements.nextElement();
          if (button.isEnabled())     // if enabled (true) 
          {
            System.out.println("This button is disabled! The text of the button is: '" + button.getText() + "'");
            button.setEnabled(false); // set it disabled (false)
          }
    }
}

Thanks @Hannibal, your post saved my day. 谢谢@Hannibal,你的帖子救了我的一天。

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

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