2017-06-07 8 views
1

誰かが私を助けてくれることを願っています。私は2 RadioMenuItemjavafxmenuを持ってこの方法:ChangeListenerを使用するToggleGroup <Toggle>は、常にoldValueにnull値をスローします

public class SiiController implements Initializable { 
private ToggleGroup companyToggleGroup; 
private RadioMenuItem companyARadio, companyBRadio; 
private MenuBar menuBar; 
private Menu fileMenu,companyMenu; 
private Boolean checked; 

@Override 
public void initialize(URL url, ResourceBundle rb) { 
/*......*/ 
    companyMenu = new Menu("Companies"); 
    companyToggleGroup = new ToggleGroup(); 
    companyARadio= new RadioMenuItem("Company A"); 
    companyARadio.setUserData("companya"); 
    companyARadio.setToggleGroup(companyToggleGroup); 
    companyARadio.setSelected(true); 
    companyBRadio= new RadioMenuItem("Company B"); 
    companyBRadio.setUserData("companyb"); 
    companyBRadio.setToggleGroup(companyToggleGroup); 
    companyMenu.getItems().addAll(companyARadio, companyBRadio); 

companyToggleGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() { 
     @Override 
     public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) { 

      if(checked){ 
       //do something 
      }else{ 
       //remains the oldValue selected 
       //The next line is just for checking the old value 
       System.out.println("oldValue UserData"+oldValue.getUserData().toString()); 
      } 

     } 
    }); 
} 
     menuBar.getMenus().addAll(fileMenu, companyMenu); 
} 

エラーは、リスナーがToggle oldValueにnull値を返すことです。私が手例外は、このいずれかになります。

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException 
at controller.SiiController$4.changed(SiiController.java:284) 
at controller.SiiController$4.changed(SiiController.java:280) 
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182) 
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81) 
at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74) 
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102) 
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112) 
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146) 
at javafx.scene.control.ToggleGroup$3.set(ToggleGroup.java:137) 
at javafx.scene.control.ToggleGroup$3.set(ToggleGroup.java:119) 
at javafx.scene.control.ToggleGroup.selectToggle(ToggleGroup.java:149) 
at javafx.scene.control.RadioMenuItem$2.invalidated(RadioMenuItem.java:195) 
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109) 
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144) 
at javafx.scene.control.RadioMenuItem.setSelected(RadioMenuItem.java:180) 
at javafx.scene.control.ToggleGroup.setSelected(ToggleGroup.java:166) 
at javafx.scene.control.ToggleGroup.access$100(ToggleGroup.java:54) 
at javafx.scene.control.ToggleGroup$3.set(ToggleGroup.java:135) 
at javafx.scene.control.ToggleGroup$3.set(ToggleGroup.java:119) 
at javafx.scene.control.ToggleGroup.selectToggle(ToggleGroup.java:149) 
at javafx.scene.control.RadioMenuItem$2.invalidated(RadioMenuItem.java:193) 
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109) 
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144) 
at javafx.scene.control.RadioMenuItem.setSelected(RadioMenuItem.java:180) 
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1401) 
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358) 
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) 
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) 
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) 
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417) 
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416) 
at com.sun.glass.ui.View.handleMouseEvent(View.java:555) 
at com.sun.glass.ui.View.notifyMouse(View.java:937) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
at java.lang.Thread.run(Thread.java:748) 

最後に、私が何をしようとしているBoolean checkedが、その後ToggleGroup遺骨の前の選択偽の場合です。あなたの時間のための おかげでたくさん

+1

'selected'プロパティは' null'に設定され、***その他の***変更イベントで新しい値に設定されているようです... – fabian

答えて

1

あなたはlistenterでこれを試してみました:

if (oldValue != null) { 
    System.out.println("old value: " + oldValue.getUserData()); 
} 
+0

ええ、私は試しましたが、私が知っている最初の 'companyARadio.setSelected(true);' – pburgov

+1

から 'companyARadio'を選択しています。私はそれが不具合かもしれないと思う。あなたのコードを実行したときも同じ問題がありました。 – Sedrick

+0

私はあなたがリジッドかもしれないと思う。私は回避策を試みます。どうもありがとう。 – pburgov

0

をこれがRadioMenuButtonで発生しますが、期待通りのRadioButtonを使用している場合、これは動作します。これにはbug reportがあります。

関連する問題