我有无线电按钮和事件侦听器在Oracle演示页面上。它工作,它将数据保存到db,但问题是,它触发的次数与群中有收音机的次数一样多。我希望它只切换一次,所以我只处理一次数据。
下面是相关代码(注意,我有许多无线电组,所以我使用地图):
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'发布于 2014-09-14 16:51:55
我修好了。正如我前面所说的,问题就像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());
}
}
}
}
}https://stackoverflow.com/questions/25830925
复制相似问题