2013-02-14 63 views
3

当为实际已经选择的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?

+0

解决方法是好的,但如果你可以考虑使用的结合,而不是制定者,你要小心:http://javafx-jira.kenai.com/browse/RT-17205。 – 2013-02-14 23:08:21

+0

幸运的是,对于我的小应用程序,我个人不会使用绑定,但感谢提示,可能对其他读者仍然有用:-) – 2013-02-14 23:10:13

+0

似乎在第一个解决方法中索引的东西根本不起作用。在我的JavaFX应用程序中,至少,'toggleGroup.getToggles()。indexOf(toggleGroup.getSelectedToggle())'总是返回-1(这就是我偶然发现这个问题的原因,因为我一直在寻找如何获得索引) 。 – 2017-10-08 17:54:08

回答

0

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

文件是针对在运行系统项目:http://javafx-jira.kenai.com

请确保您有一个链接回这种情况下,提供一些可重复的示例代码,因为它似乎有些similar issues已经被关闭,因为开发商无法重现问题。

这里是一个复制只使用Java和无FXML的bug一个一些示例代码 - 使用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 
+1

http://javafx-jira.kenai.com/browse/RT-28412 – 2013-02-14 22:57:03

+0

谢谢你们两位!我想ToggleGroup.selectToggle不执行一些ChangeListener,因为选择一个已经选择的RadioButton不是一个改变,但仍然有一些其他的监听器被执行或谁知道还有什么......任何方式感谢将它转换为普通的Java并发布它,我们所有人都很好的团队合作:P – 2013-02-14 23:07:56