0

i have four JRatioButtons inside a ButtonGroup in Java. 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.

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!

Hannibal
  • 15
  • 1
  • 1
  • 7
  • 1
    You simply need to keep references to either the `ButtonGroup` objects or the `jRadioButton` objects themselves, and disable them when necessary. `ButtonGroup` provides a `getElements()` method – Brian Roach Oct 24 '12 at 00:25
  • You're probably looking for the _enabled_ property. – trashgod Oct 24 '12 at 00:49
  • I edited the question, as you can see i try to use the getElements() method for the ButtonGroup, and i can find what button is disabled and bring the text using the console, but i cant use the setEnabled method to disable the jRadioButton.... Dont know why... thanks for the help – Hannibal Oct 24 '12 at 01:02
  • I have a mistake in the code, actually is working but i was trying to enable an already enabled button.... i will post the answer later – Hannibal Oct 24 '12 at 01:18

3 Answers3

1

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.

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);
}
seanxiaoxiao
  • 1,282
  • 2
  • 11
  • 23
  • Hey sean if i go directly for the JrRadioButton reference the editor says "it cannot be resolve to a type" error. I think is because the buttons are inside the ButtonGroup, actually i need to access the buttons inside the ButtonGroup to do "something" with them... thanks for the help – Hannibal Oct 24 '12 at 01:00
  • I just checked the API, the button group has a method called getElements() which returns all the buttons that are participating in this group. You can iterate in this collections. – seanxiaoxiao Oct 24 '12 at 01:17
1

Try this, it works.

AbstractButton button = ...

button.getModel().setEnabled(true/false)
Yasas
  • 82
  • 2
0

Ya, you setEnabled(true) to an enabled 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.

sky91
  • 3,060
  • 2
  • 19
  • 26