当为实际已经选择的RadioButton调用ToggleGroup.selectToggle(Toggle toggle)
时,该RadioButton变为未选中状态。我觉得这是一个错误,任何人都可以证实这一点?ToggleGroup.select切割错误?
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());
}
}
代码也在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(!):
接取的切换后面的控制,例如一个RadioButton,并以编程方式取消选中该选项。见Link between Toggle and e.g. the RadioButton behind it?。
解决方法是好的,但如果你可以考虑使用的结合,而不是制定者,你要小心:http://javafx-jira.kenai.com/browse/RT-17205。 – 2013-02-14 23:08:21
幸运的是,对于我的小应用程序,我个人不会使用绑定,但感谢提示,可能对其他读者仍然有用:-) – 2013-02-14 23:10:13
似乎在第一个解决方法中索引的东西根本不起作用。在我的JavaFX应用程序中,至少,'toggleGroup.getToggles()。indexOf(toggleGroup.getSelectedToggle())'总是返回-1(这就是我偶然发现这个问题的原因,因为我一直在寻找如何获得索引) 。 – 2017-10-08 17:54:08