简体   繁体   English

JavaFX中的ToggleButtons和侦听器

[英]ToggleButtons and listeners in JavaFX

I have this code 我有这个代码

@FXML
private ToggleButton tb1;
@FXML
private ToggleButton tb2;
@FXML
ToggleGroup group = new ToggleGroup();

String cpuLoad1 ="D:/myWorkspace/TestAttacks/input_folder/app_debug.apk";
String cpuLoad2 = "D:/myWorkspace/TestAttacks/input_folder/cpuLoad1.apk";   

@FXML
private void onToggleClick(){

    tb1.setUserData(cpuLoad1);
    tb1.setToggleGroup(group);
    tb2.setUserData(cpuLoad2);
    tb2.setToggleGroup(group);

    ChangeListener<Toggle> cLt = new ChangeListener<Toggle>(){
        public void changed(ObservableValue<? extends Toggle> ov,
                Toggle toggle, Toggle new_toggle){
            if (new_toggle != null){
                System.out.println(group.getSelectedToggle().getUserData().toString());

            }else{
                System.out.println("hello no");
            }
        }
    };

    group.selectedToggleProperty().addListener(cLt);

}

Although I am still not using the userdata, the thing is that whenever I click the toggle button I get the desired output in increasing order. 尽管我仍未使用userdata,但事实是,每当我单击切换按钮时,我都会获得升序的所需输出。

Here is the output: 这是输出:

hello no                                                 //click2
D:/myWorkspace/TestAttacks/input_folder/app_debug.apk    //click3
D:/myWorkspace/TestAttacks/input_folder/app_debug.apk
hello no                                                 //click4
hello no
hello no
D:/myWorkspace/TestAttacks/input_folder/app_debug.apk    //click5
D:/myWorkspace/TestAttacks/input_folder/app_debug.apk
D:/myWorkspace/TestAttacks/input_folder/app_debug.apk
D:/myWorkspace/TestAttacks/input_folder/app_debug.apk
hello no                                                 //click6
hello no
hello no
hello no
hello no
D:/myWorkspace/TestAttacks/input_folder/app_debug.apk    //click7
D:/myWorkspace/TestAttacks/input_folder/app_debug.apk
D:/myWorkspace/TestAttacks/input_folder/app_debug.apk
D:/myWorkspace/TestAttacks/input_folder/app_debug.apk
D:/myWorkspace/TestAttacks/input_folder/app_debug.apk
D:/myWorkspace/TestAttacks/input_folder/app_debug.apk

In first click I get nothing. 在第一次单击中,我什么也没得到。

From second click I start getting this kind of output. 从第二次单击开始,我开始获得这种输出。 Could anyone explain this behaviour and provide me a solution for this? 谁能解释这种行为并为我提供解决方案?

All the code from the onToggleClick method should go to the initialize method of the controller. onToggleClick方法中的所有代码都应转到控制器的initialize方法。

Now, on the first toggle click you set the data for the ToggleButton s and put them into the group and then you set the listener on the ToggleGroup . 现在,在第一个切换上单击,设置ToggleButton的数据并将其放入组中,然后在ToggleGroup上设置侦听ToggleGroup So this listener will be executed only on the second toggle-click, where you set the data again and -even worse- you add an additional listener. 因此,仅在第二次切换单击时执行此侦听器,在此您再次设置数据,甚至更糟的是,添加了另一个侦听器。

On the third click, these two listeners will be executed and you add another one ... hence the growing list of "... .apk" prints on the console. 第三次单击时,将执行这两个侦听器,并添加另一个侦听器...,因此控制台上打印的“ .... apk”列表不断增加。

All of these action should happen only once, after all of the nodes are created: this place is the initialize method of the controller. 创建所有节点后,所有这些操作应只发生一次:此位置是控制器的initialize方法。


If you would like to have independent ToggleButton s, simply do not put the ToggleButton s into a ToggleGroup , and then you can listen to the selectedProperty of the toggles separately: 如果您希望拥有独立的ToggleButton ,则只需将ToggleButton放入ToggleGroup ,然后可以分别收听切换的selectedProperty

ToggleButton tb1 = new ToggleButton("ToggleButton1");
ToggleButton tb2 = new ToggleButton("ToggleButton2");

tb1.selectedProperty().addListener(((observable, oldValue, newValue) -> {
    System.out.println(tb1.getText() + " changed from " + oldValue + " to " + newValue);
    System.out.println("Taking a nap!");
}));

tb2.selectedProperty().addListener(((observable, oldValue, newValue) -> {
    System.out.println(tb2.getText() + " changed from " +oldValue + " to " + newValue);
    System.out.println("Working hard!");
}));

Alternatively you can also use the onActionProperty . 或者,您也可以使用onActionProperty

for (int i = 0; i< 20; i++) {
    ToggleButton tb = new ToggleButton("ToggleButton"+i);
    tb.setUserData("UserData"+i);
    tb.setOnAction(e -> {
        System.out.println(tb.getText() + " - Selected: " + tb.isSelected() 
            + "; UserData: " + tb.getUserData());
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM