2011-12-06 4 views
4

ラジオのトップレベルメニューに2つのラジオMenuItemが含まれています。私は両方のためにSelectionListenerを追加します。ラジオメニュー項目SelectionListenerを2回生成 - SWT

MenuItem radio = new MenuItem(bar, SWT.CASCADE); /* bar is the menu bar */ 
    radio.setText("Radio"); 

    Menu menu = new Menu(radio); 
    radio.setMenu(menu); 

    MenuItem mntmOption_1 = new MenuItem(menu, SWT.RADIO); 
    mntmOption_1.addSelectionListener(new SelectionAdapter() { 
     @Override 
     public void widgetSelected(SelectionEvent e) { 
      System.out.println("Option 1 selected"); 
     } 
    }); 
    mntmOption_1.setText("Option1"); 

    MenuItem mntmOption_2 = new MenuItem(menu, SWT.RADIO); 
    mntmOption_2.addSelectionListener(new SelectionAdapter() { 
     @Override 
     public void widgetSelected(SelectionEvent e) { 
      System.out.println("Option 2 selected"); 
     } 
    }); 
    mntmOption_2.setText("Option2"); 

最初に私はそれを示しmntmOption_1を選択:

Option1 selected 

その後、私はそれを示してmntmOption_2を選択:

Option1 selected 
Option2 selected 

それは両方のリスナーを発射されているようです。ここに質問があります:なぜですか?私はWinXPを実行しています。

答えて

8

2つ目のラジオボタンが選択を失うので、両方のリスナーが発砲しています。特定の状態にのみ反応する場合は、ウィジェットの状態をチェックする必要があります。

1

実装中、私は選択と問題の選択を解除するために、ラジオボタンのトリガダブルイベントリスナーを見つけた。追加問題を解決するために

 boolean isSelected = ((Button)e.getSource()).getSelection(); 
     if(isSelected){ 
      ....     
     } 

例:

buttonRd0 = new Button(parent, SWT.RADIO); 
button.addSelectionListener(new SelectionAdapter() { 
    @Override 
    public void widgetSelected(SelectionEvent e) 
    { 
     boolean isSelected = ((Button)e.getSource()).getSelection(); 
     if(isSelected){ 
      ....     
     } 
    } 
}); 
関連する問題