Prevent a toggle group from not having a toggle selected - Java FX
Asked Answered
L

2

11

I have a toggle group with two toggle buttons that should look like this (Yellow toggle is the selected toggle).

enter image description here

However when I click on the selected toggle both toggles become unselected and look like this.

enter image description here

Then if I try to get whether the toggle is selected, I get a nullPointerException.

(Boolean) toggleGroup.getSelectedToggle().getUserData();

Is it possible to prevent the selected toggle from being unselected when it is clicked?

Lucier answered 19/10, 2017 at 16:54 Comment(1)
RadioButtons have the functionality you're looking for. Can you use those instead of ToggleButtons?Vaudeville
I
11

You can try the following code to create a persistent toggle.

/**
 * Create a toggle group of buttons where one toggle will always remain switched on.
 */
class PersistentButtonToggleGroup extends ToggleGroup {
  PersistentButtonToggleGroup() {
    super();
    getToggles().addListener(new ListChangeListener<Toggle>() {
        @Override public void onChanged(Change<? extends Toggle> c) {
        while (c.next()) {
          for (final Toggle addedToggle : c.getAddedSubList()) {
            ((ToggleButton) addedToggle).addEventFilter(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() {
              @Override public void handle(MouseEvent mouseEvent) {
                if (addedToggle.equals(getSelectedToggle())) mouseEvent.consume();
              }
            });
          }
        }
      }
    });
  }
}

This just reacts to mouse events, so perhaps not ideal if you want to account for keyboard events or changing toggles in code.


A similar, but perhaps more complete approach that I haven't tried may be defined in the blog entry Button of Choice: Use ToggleButtons as RadioButtons.


Perhaps the simplest behavior to accomplish what you desire might be to just use RadioButtons instead of ToggleButtons.

If, additionally, you want the buttons styled like ToggleButtons, instead of RadioButtons, then you can try the styling technique which is outlined in: How to make a RadioButton look like regular Button in JavaFX.

RadioButton radioButton=new RadioButton("Radio");
radioButton.getStyleClass().remove("radio-button");
radioButton.getStyleClass().add("toggle-button");
Infeudation answered 19/10, 2017 at 23:3 Comment(0)
B
22

This one works for me. If the new selected element is null that means theres no selected element, therefore just select the previous one (which is the "oldValue")

toggleGroup.selectedToggleProperty().addListener((obsVal, oldVal, newVal) -> {
    if (newVal == null)
        oldVal.setSelected(true);
});
Bk answered 3/6, 2018 at 13:57 Comment(1)
very nice and quick approach. Thanks!Vernitavernoleninsk
I
11

You can try the following code to create a persistent toggle.

/**
 * Create a toggle group of buttons where one toggle will always remain switched on.
 */
class PersistentButtonToggleGroup extends ToggleGroup {
  PersistentButtonToggleGroup() {
    super();
    getToggles().addListener(new ListChangeListener<Toggle>() {
        @Override public void onChanged(Change<? extends Toggle> c) {
        while (c.next()) {
          for (final Toggle addedToggle : c.getAddedSubList()) {
            ((ToggleButton) addedToggle).addEventFilter(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() {
              @Override public void handle(MouseEvent mouseEvent) {
                if (addedToggle.equals(getSelectedToggle())) mouseEvent.consume();
              }
            });
          }
        }
      }
    });
  }
}

This just reacts to mouse events, so perhaps not ideal if you want to account for keyboard events or changing toggles in code.


A similar, but perhaps more complete approach that I haven't tried may be defined in the blog entry Button of Choice: Use ToggleButtons as RadioButtons.


Perhaps the simplest behavior to accomplish what you desire might be to just use RadioButtons instead of ToggleButtons.

If, additionally, you want the buttons styled like ToggleButtons, instead of RadioButtons, then you can try the styling technique which is outlined in: How to make a RadioButton look like regular Button in JavaFX.

RadioButton radioButton=new RadioButton("Radio");
radioButton.getStyleClass().remove("radio-button");
radioButton.getStyleClass().add("toggle-button");
Infeudation answered 19/10, 2017 at 23:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.