當爲實際已經選擇的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