3

当调用ToggleGroup.selectToggle(Toggle toggle)实际上已被选中的 RadioButton 时,此 RadioButton 将变为未选中状态。我觉得这是一个错误,任何人都可以确认这一点吗?

切换.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>

切换演示控制器:

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


}

代码也可在http://codestefan.googlecode.com/svn/trunk/ToggleDemo

感谢您的任何提示!

更新:

这是我想出的解决方法:

代替

myToggleGroup.selectToggle(myToggleGroup.getSelectedToggle());

利用

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

或者换句话说:而不是ToggleGroup.selectToggle使用Toggle.setSelected. 猜猜在这种情况下不需要所有索引的东西,但是给定一个存储在数据库中的索引,我需要在恢复我的应用程序时选择一个切换,所以这针对我的情况进行了调整。

可能(!)解决方法2:

访问 Toggle 后面的控件,例如 RadioButton,然后以编程方式取消选择该控件。请参阅Toggle 和它背后的 RadioButton 之间的链接?.

4

1 回答 1

0

是的。你发现了一个错误。

将其与运行时项目归档在:http: //javafx-jira.kenai.com

确保您包含返回此案例的链接,该链接提供了一些可重现的示例代码,因为似乎一些类似的问题已经关闭,因为开发人员无法重现该问题。

这是一些示例代码,它仅使用 Java 而不使用 FXML 来复制错误 - JavaFX 2.2.5 和 Java8b76 的行为是奇怪和错误的 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); }
}

重复按键后的程序输出:

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
于 2013-02-14T22:55:24.363 回答