ToggleButtons and listeners in JavaFX

主宰稳场 提交于 2019-12-20 04:24:16

问题


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.

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?


回答1:


All the code from the onToggleClick method should go to the initialize method of the controller.

Now, on the first toggle click you set the data for the ToggleButtons and put them into the group and then you set the listener on the 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.

All of these action should happen only once, after all of the nodes are created: this place is the initialize method of the controller.


If you would like to have independent ToggleButtons, simply do not put the ToggleButtons into a ToggleGroup, and then you can listen to the selectedProperty of the toggles separately:

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.

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());
    });
}


来源:https://stackoverflow.com/questions/42413313/togglebuttons-and-listeners-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!