How to make RadioButton look like a regular button in JavaFX

I implement a toolbar similar to a toolbar, so the user can only select one tool at a time, and I switched from Buttonto RadioButtonfor its behavior.

But I found that I was RadioButtonusing my own dot skin, however I still want it to display as normal Button. I'm new to JavaFX and FXML, so does anyone know how I can do this?

+4
source share
4 answers

First create a radio button , remove the style radio-button, and then add a style toggle-buttonlike

RadioButton radioButton=new RadioButton("Radio");
radioButton.getStyleClass().remove("radio-button");
radioButton.getStyleClass().add("toggle-button");

, .

+6
-. ToggleButton , RadioButton. weird click .
public class RadioToggleButton extends ToggleButton {

    // As in RadioButton.
    /**
     * Toggles the state of the radio button if and only if the RadioButton
     * has not already selected or is not part of a {@link ToggleGroup}.
     */
    @Override
    public void fire() {
        // we don't toggle from selected to not selected if part of a group
        if (getToggleGroup() == null || !isSelected()) {
            super.fire();
        }
    }
}

FXML:

<fx:define>
    <ToggleGroup fx:id="toolToggleGroup" />
</fx:define>
<RadioToggleButton toggleGroup="$toolToggleGroup" />
<RadioToggleButton toggleGroup="$toolToggleGroup" />

.

+2

, ,

.radio-button > .radio {
  -fx-background-color: transparent;
  -fx-background-insets: 0;
  -fx-background-radius: 0px;
  -fx-padding: 0.0em; /* 4 8 4 8 */
}
.radio-button:selected > .radio > .dot {
  -fx-background-color: transparent;
}

; , - .

+1

ToggleButton ToggleGroup, ToggleGroup, . .

0

Source: https://habr.com/ru/post/1616407/


All Articles