首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使JavaFX无线电按钮事件只触发一次

使JavaFX无线电按钮事件只触发一次
EN

Stack Overflow用户
提问于 2014-09-14 07:08:17
回答 1查看 585关注 0票数 0

我有无线电按钮和事件侦听器在Oracle演示页面上。它工作,它将数据保存到db,但问题是,它触发的次数与群中有收音机的次数一样多。我希望它只切换一次,所以我只处理一次数据。

下面是相关代码(注意,我有许多无线电组,所以我使用地图):

代码语言:javascript
运行
复制
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时,输出是:

代码语言:javascript
运行
复制
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'

我只想做一次,所以:

代码语言:javascript
运行
复制
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'
EN

回答 1

Stack Overflow用户

发布于 2014-09-14 16:51:55

我修好了。正如我前面所说的,问题就像Uluk指出的那样--每次我调用这个函数时,我都创建了事件侦听器。

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

代码语言:javascript
运行
复制
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());
                }
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25830925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档