実際にすでに選択されている RadioButton を呼び出すToggleGroup.selectToggle(Toggle toggle)
と、この 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>
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:
Toggle の背後にあるコントロール (RadioButton など) にアクセスし、プログラムでそのコントロールを選択解除します。Toggle とその背後にある RadioButton の間のリンクを参照してください。.