Search code examples
eventsjavafxradio-buttononchangeradio-group

Radio Button Change Event JavaFx


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


Solution

  • 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?