JavaFX中的ToggleButtons和侦听器 [英] ToggleButtons and listeners in JavaFX

查看:225
本文介绍了JavaFX中的ToggleButtons和侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码

@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);

}

虽然我还没有使用userdata,但事情是每当我点击切换按钮时,我会按递增顺序获得所需的输出。

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.

这是输出:

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?

推荐答案

onToggleClick中的所有代码方法应该转到控制器的 initialize 方法。

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

现在,在第一个切换单击您设置 ToggleButton 的数据并将它们放入组中,然后在 ToggleGroup
所以这个监听器只会在第二个切换点击时执行,你再次设置数据,甚至更糟 - 你添加一个额外的监听器。

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.

在第三次单击时,将执行这两个侦听器并添加另一个...因此,控制台上会显示... .apk的增长列表。

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.

如果您想拥有独立的 ToggleButton ,只需不要放将ToggleButton 转换为 ToggleGroup ,然后您可以收听 selectedProperty 分别切换:

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!");
}));

或者您也可以使用 onActionProperty

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

这篇关于JavaFX中的ToggleButtons和侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆