Radio Button Change Event JavaFx


Question

i have a lot of HBoxes with some different components inside ( RadioButton, Labels, Buttons ...). The RadioButtons are in a ToggleGroup, so only 1 Radio Button can be selected.

I want to add a OnChange - Event to the Radio Button. If the RadioButton would be unselected there should be a Event-Trigger. How can i add the Event to the Radio-Button?

At the moment i have a code like this, but it doesn't have the function i want to have.

radio.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent arg0) {
            if(!radio.isSelected()){
                ivTriangleImg.setRotate(iRotateCoord2);
                btnTriangle.setGraphic(ivTriangleImg);
            }

            if(group!=null){
                group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

                    @Override
                    public void changed(
                            ObservableValue<? extends Toggle> arg0,
                            Toggle arg1, Toggle arg2) {

                    }
                });
            }
        }
    });

I use JavaFX 2.0 and Java 1.7 so i can not use Lambda Functions or the special component functions of JavaFx8 / Java 1.8

1
0
10/23/2014 7:43:19 AM

Accepted Answer

radio.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent arg0) {
            if(!radio.isSelected()){
                ivTriangleImg.setRotate(iRotateCoord2);
                btnTriangle.setGraphic(ivTriangleImg);
            }

            if(group!=null){
                group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

                    @Override
                    public void changed(
                            ObservableValue<? extends Toggle> arg0,
                            Toggle arg1, Toggle arg2) {

                        if(!radio.isSelected()&&ivTriangleImg.getRotate()!=iRotateCoord1){
                            ivTriangleImg.setRotate(iRotateCoord1);
                            btnTriangle.setGraphic(ivTriangleImg);
                        }

                    }
                });
            }
        }
    });

This would work for my Question. I have done a little mistake, so i don't check the Radio-Style. It works fine now... Sorry for this Question.

Is there a possibility to check the Event at the Radio Button and not at his group?

1
10/23/2014 8:04:36 AM

The state of JavaFX controls is represented by observable properties. You can access these properties with control.propertyNameProperty() and add ChangeListeners to them:

radioButton.selectedProperty().addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue<? extends Boolean> obs, Boolean wasPreviouslySelected, Boolean isNowSelected) {
        if (isNowSelected) { 
            // ...
        } else {
            // ...
        }
    }
});

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon