2014-09-14 4 views
0

Oracle 데모 페이지에서 라디오 버튼과 이벤트 리스너를 사용할 수 있습니다. 그것은 작동한다, 그것은 데이터를 db에 저장하지만, 문제는 그룹에 라디오가있는 것처럼 여러 번 발사된다는 것이다. 한 번만 데이터를 처리 할 수 ​​있도록 한 번만 토글하고 싶습니다.JavaFX 라디오 버튼 이벤트를 한 번만 실행하십시오.

여기에 관련 코드입니다 (나는지도를 사용하고, 그래서 나는 많은 라디오 그룹이 주) :

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: [email protected][styleClass=radio-button]'b'], old_tgl:[email protected][styleClass=radio-button]'a' and new_tgl: [email protected][styleClass=radio-button]'b' 
Called with ov:ReadOnlyObjectProperty [value: [email protected][styleClass=radio-button]'b'], old_tgl:[email protected][styleClass=radio-button]'a' and new_tgl: [email protected][styleClass=radio-button]'b' 
Called with ov:ReadOnlyObjectProperty [value: [email protected][styleClass=radio-button]'b'], old_tgl:[email protected][styleClass=radio-button]'a' and new_tgl: [email protected][styleClass=radio-button]'b' 
Called with ov:ReadOnlyObjectProperty [value: [email protected][styleClass=radio-button]'b'], old_tgl:[email protected][styleClass=radio-button]'a' and new_tgl: [email protected][styleClass=radio-button]'b' 
Called with ov:ReadOnlyObjectProperty [value: [email protected][styleClass=radio-button]'b'], old_tgl:[email protected][styleClass=radio-button]'a' and new_tgl: [email protected][styleClass=radio-button]'b' 

내가 그렇게, 그것은 단지 일단 싶습니다 :

Called with ov:ReadOnlyObjectProperty [value: [email protected][styleClass=radio-button]'b'], old_tgl:[email protected][styleClass=radio-button]'a' and new_tgl: [email protected][styleClass=radio-button]'b' 
+0

내가 문제를 복제하려고했지만 할 수 없었다 다음을 radioGroup은 (처음으로) 생성되는 경우

수정 체크를 추가하는 것입니다. 나는 당신이 당신의 코드에서 무엇을하는지보고 싶다. 이 작업을 위해 [MCVE] (http://stackoverflow.com/help/mcve)를 만들 수 있습니까? – ItachiUchiha

+1

'RadioGroups.get (group) ... '코드를 디버그하십시오. 동일한 속성에 동일한 수신기를 두 번 이상 추가 할 수 있습니다. –

+0

그래, 그게 문제 였어 - 내가 DrawRadio를 부를 때마다 나는 이벤트 리스너를 추가했다! – May11

답변

0

을 나는 f를 그것을 가지고 ixed. 앞서 말했듯이 Uluk이 지적했듯이 문제는 내가 함수를 호출 할 때마다 이벤트 리스너를 만들었습니다.

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()); 
       } 
      } 
     } 
    } 
} 
관련 문제