ToggleGroup.selectToggle error?

When called ToggleGroup.selectToggle(Toggle toggle)

for a RadioButton that is actually already selected, that RadioButton becomes unselected. I feel like this is a bug, can anyone confirm this?

toggle.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<VBox prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.ToggleDemoController">
  <children>
    <RadioButton mnemonicParsing="false" selected="true" text="First RadioButton">
      <toggleGroup>
        <ToggleGroup fx:id="myToggleGroup" />
      </toggleGroup>
    </RadioButton>
    <RadioButton mnemonicParsing="false" text="Second RadioButton" toggleGroup="$myToggleGroup" />
  </children>
</VBox>

      

ToggleDemoController:

package com.example;

import javafx.fxml.FXML;
import javafx.scene.control.ToggleGroup;

public class ToggleDemoController 
{
    @FXML
    private ToggleGroup myToggleGroup;

    // Implementing Initializable Interface no longer required according to
    // http://docs.oracle.com/javafx/2/fxml_get_started/whats_new2.htm
    @SuppressWarnings("unused") // only called by FXMLLoader
    @FXML
    private void initialize()
    {
        // Select the currently selected toggle (that is the first RadioButton) again.
        // This unselects the first RadioButton, while one would expect it to stay selected.
        myToggleGroup.selectToggle(myToggleGroup.getSelectedToggle());
    }


}

      

The code is also available at http://codestefan.googlecode.com/svn/trunk/ToggleDemo

Thanks for any hint!

Update:

Here is a workaround I figured out:

Instead

myToggleGroup.selectToggle(myToggleGroup.getSelectedToggle());

      

using

Toggle selectedToggle = myToggleGroup.getSelectedToggle();
int selectedToggleIndex = myToggleGroup.getToggles().indexOf(selectedToggle);
myToggleGroup.getToggles().get(selectedToggleIndex).setSelected(true);

      

or in other words: ToggleGroup.selectToggle

Use instead Toggle.setSelected

. Guess it doesn't need all the index stuff in this case, but given the index stored in the database, I need to select Toggle when restoring my application, so that's adjusted for my case.

Probably (!) Workaround 2:

Eliminate control over Toggle, for example. RadioButton and deselect programmatically. See the link between Toggle and for example the RadioButton behind it? ...

+3


source to share


1 answer


Yes. You found a bug.

Load it into your project at runtime: http://javafx-jira.kenai.com

Make sure you include a link to this case, which provides some reproducible example code, as it seems like some similar issues are already closed since the developers were unable to reproduce the issue.

Here is some sample code that replicates the error using only Java and not FXML - the behavior with JavaFX 2.2.5 and Java8b76 is strange and buggy. IMO:



import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ToggleTest extends Application {
    @Override public void start(Stage primaryStage) {
        final ToggleGroup tg = new ToggleGroup();
        RadioButton radio1 = new RadioButton("1");
        radio1.setId("1");
        radio1.setToggleGroup(tg);
        RadioButton radio2 = new RadioButton("2");
        radio2.setId("2");
        radio2.setToggleGroup(tg);

        tg.selectToggle(radio1);
//        radio1.setSelected(true);

        Button button = new Button("Select 1");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent t) {
                System.out.println("Selected Toggle Before Select: " + tg.getSelectedToggle() + " isSelected? " + tg.getSelectedToggle().isSelected());
                tg.selectToggle(tg.getSelectedToggle());
                System.out.println("Selected Toggle After Select:  " + tg.getSelectedToggle() + " isSelected? " + tg.getSelectedToggle().isSelected());
            }
        });

        VBox layout = new VBox(10);
        layout.getChildren().addAll(radio1, radio2, button);
        layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");

        primaryStage.setScene(new Scene(layout));
        primaryStage.show();
    }
    public static void main(String[] args) { launch(args); }
}

      

Exiting the program after pressing the buttons several times:

Selected Toggle Before Select: RadioButton[id=1, styleClass=radio-button] isSelected? true
Selected Toggle After Select:  RadioButton[id=1, styleClass=radio-button] isSelected? false
Selected Toggle Before Select: RadioButton[id=1, styleClass=radio-button] isSelected? false
Selected Toggle After Select:  RadioButton[id=1, styleClass=radio-button] isSelected? false
Selected Toggle Before Select: RadioButton[id=1, styleClass=radio-button] isSelected? false
Selected Toggle After Select:  RadioButton[id=1, styleClass=radio-button] isSelected? false

      

0


source







All Articles