How to make a RadioButton look like regular Button in JavaFX

后端 未结 4 2169
耶瑟儿~
耶瑟儿~ 2020-12-18 07:04

I\'m implementing a toolbox-like pane, so user can only pick one tool at a time, and I switched from Button to RadioButton for its behavior.

<
4条回答
  •  暖寄归人
    2020-12-18 07:27

    Finally I went with another way around. You can extend ToggleButton so that it behaves like a RadioButton. This does not have the weird click effect of consuming mouse release.

    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();
            }
        }
    }
    

    Then in FXML:

    
        
    
    
    
    

    And it is done.

提交回复
热议问题