使JavaFX radiobutton事件只触发一次

时间:2014-09-14 07:08:17

标签: javafx toggle radio

我在Oracle演示页面上有radiobuttons和事件监听器。 它工作,它将数据保存到数据库,但问题是,它发射的次数与组中的无线电一样多。 我希望它只能切换为ONCE,所以我只处理数据一次。

这是相关代码(注意我有很多广播组,所以我使用的是地图):

private Map<Integer, RadioButton> Radios = new HashMap<>();
private Map<Integer, ToggleGroup> RadioGroups = new HashMap<>();

(...)
DrawRadio (int group, int id, String label) {

    if (RadioGroups.get(group) == null) {
        RadioGroups.put(group, new ToggleGroup());
    }

    (...)

    Radios.put(id, new RadioButton(label));
    Radios.get(id).setToggleGroup(RadioGroups.get(group));
    Radios.get(id).setUserData(ans_id);

    (...)

    RadioGroups.get(group).selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
        public void changed(ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) {

            if (RG.getSelectedToggle().equals(new_toggle)) {
                System.out.println ("Called with ov:" + ov + ", old_tgl:" + old_toggle.toString() + " and new_tgl: " + new_toggle.toString());
            }
        }
    }
}

当我将选定的无线电从a更改为b时的输出是:

Called with ov:ReadOnlyObjectProperty [value: RadioButton@3814fb6f[styleClass=radio-button]'b'], old_tgl:RadioButton@5b417598[styleClass=radio-button]'a' and new_tgl: RadioButton@3814fb6f[styleClass=radio-button]'b'
Called with ov:ReadOnlyObjectProperty [value: RadioButton@3814fb6f[styleClass=radio-button]'b'], old_tgl:RadioButton@5b417598[styleClass=radio-button]'a' and new_tgl: RadioButton@3814fb6f[styleClass=radio-button]'b'
Called with ov:ReadOnlyObjectProperty [value: RadioButton@3814fb6f[styleClass=radio-button]'b'], old_tgl:RadioButton@5b417598[styleClass=radio-button]'a' and new_tgl: RadioButton@3814fb6f[styleClass=radio-button]'b'
Called with ov:ReadOnlyObjectProperty [value: RadioButton@3814fb6f[styleClass=radio-button]'b'], old_tgl:RadioButton@5b417598[styleClass=radio-button]'a' and new_tgl: RadioButton@3814fb6f[styleClass=radio-button]'b'
Called with ov:ReadOnlyObjectProperty [value: RadioButton@3814fb6f[styleClass=radio-button]'b'], old_tgl:RadioButton@5b417598[styleClass=radio-button]'a' and new_tgl: RadioButton@3814fb6f[styleClass=radio-button]'b'

我希望它只是一次,所以:

Called with ov:ReadOnlyObjectProperty [value: RadioButton@3814fb6f[styleClass=radio-button]'b'], old_tgl:RadioButton@5b417598[styleClass=radio-button]'a' and new_tgl: RadioButton@3814fb6f[styleClass=radio-button]'b'

1 个答案:

答案 0 :(得分:0)

我修好了。正如我之前所说,问题就像Uluk所指出的那样 - 每当我调用函数时,我都会创建事件监听器。

修复是添加检查是否生成RadioGroup(第一次):

DrawRadio (int group, int id, String label) {

    boolean AddListener = false;

    if (RadioGroups.get(group) == null) {
        AddListener = true;
        RadioGroups.put(group, new ToggleGroup());
    }

    (...)
    if (AddListener == true) {
        RadioGroups.get(group).selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
            public void changed(ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) {

                if (RG.getSelectedToggle().equals(new_toggle)) {
                    System.out.println ("Called with ov:" + ov + ", old_tgl:" + old_toggle.toString() + " and new_tgl: " + new_toggle.toString());
                }
            }
        }
    }
}