当为实际上已选择的RadioButton调用ToggleGroup.selectToggle(Toggle toggle)
时,该RadioButton将变为未选中状态。我觉得这是一个错误,有人可以确认吗?
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>
切换演示 Controller :
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);
或者换句话说:使用Toggle.setSelected
代替ToggleGroup.selectToggle
。我想在这种情况下不需要所有索引内容,但给定存储在数据库中的索引,我需要选择一个恢复应用程序的切换,因此这会根据我的情况进行调整。
可能(!)解决方法 2:
访问切换背后的控件,例如一个 RadioButton,然后以编程方式取消选择该按钮。请参阅Link between Toggle and e.g. the RadioButton behind it? .
最佳答案
是的。您发现了一个错误。
针对运行时项目将其归档:http://javafx-jira.kenai.com
确保您包含返回此案例的链接,该链接提供了一些可重现的示例代码,因为它看起来有些 similar issues由于开发人员无法重现该问题,该问题已被关闭。
下面是一些示例代码,仅使用 Java 而没有使用 FXML 来复制错误 - JavaFX 2.2.5 和 Java8b76 的行为在我看来很奇怪且有错误:
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
关于ToggleGroup.selectToggle 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14885346/