2013-05-08 80 views
7

如何捕獲fxml組合框的選定值並將其實現爲javafx類?fxml組合框,將選定的值轉換爲javafx

我給了combobox fx:id「sample」,並創建了一個按鈕,其中onAction =「#test」並嘗試.getValue和.getPromptText。

@FXML private ComboBox<String> Sample; 

@FXML protected void test(ActionEvent event) { 
String output = (String) Sample.getValue(); 
System.out.println(output); 

String output = (String) Sample.getPromptText(); 
System.out.println(output); 
} 

如果我嘗試運行它,我得到一個錯誤:事先

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1440) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53) 
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28) 
    at javafx.event.Event.fireEvent(Event.java:171) 
    at javafx.scene.Node.fireEvent(Node.java:6863) 
    at javafx.scene.control.Button.fire(Button.java:179) 
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193) 
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336) 
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53) 
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33) 
    at javafx.event.Event.fireEvent(Event.java:171) 
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3324) 
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3164) 
    at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3119) 
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1559) 
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2261) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:228) 
    at com.sun.glass.ui.View.handleMouseEvent(View.java:528) 
    at com.sun.glass.ui.View.notifyMouse(View.java:922) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29) 
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73) 
    at java.lang.Thread.run(Thread.java:722) 
Caused by: java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1435) 
    ... 45 more 
Caused by: java.lang.NullPointerException 
    at TW_JAVAFX_Undecorator.ButtonController.pruefen(ButtonController.java:60) 
    ... 50 more 

感謝

殭屍

回答

6

我覺得你在你的問題有代碼應工作,只要因爲代碼中的組合框標識符與您的fxml fx:id相匹配。

我修改了這個JavaFX fxml combo box selection demonstration app來添加一個帶有onAction方法的按鈕,使用comboBox getValue()方法從組合框中檢索一個值,它對我來說工作得很好。

檢查的事情時,我注意到你說的fx:idsample,但在你的代碼中使用Sample - 和案件必須匹配,否則FXML裝載機將無法正常注入節點添加到您的控制器。

很難說您的代碼中的NullPointerException與您的組合框值檢索問題有關,因爲您沒有說明TW_JAVAFX_Undecorator.ButtonController.pruefen(ButtonController.java:60)上的代碼是或提供了完整的可執行代碼來複制該問題。

25

試試這個:

String output = Sample.getSelectionModel().getSelectedItem().toString(); 
System.out.println(output); 
+0

這幫了我!我正在尋找正確的方法來獲取選定的索引和項目,因爲getValue在其他地方,所選項目在這裏。 – 2015-08-03 08:05:28

+0

Sample.getSelectionModel()。getSelectedIndex() – QuadX 2015-10-17 15:02:35

+3

投射是多餘的。 toString()始終是String類型。 – Zon 2016-01-10 09:57:31

2

要獲取ComboBox中選定的值,你可以使用Sample.getSelectionModel方法。

例子:

myComboBox.getSelectionModel().selectedItemProperty() 
    .addListener(new ChangeListener<String>() { 
     public void changed(ObservableValue<? extends String> observable, 
          String oldValue, String newValue) { 
      System.out.println("Value is: "+newValue); 
     } 
}); 
+0

感謝它爲我工作。 – 2017-03-22 18:04:52

0

我試圖找到這個錯誤的答案(剛剛發生在我身上在相同條件下),並發現這個職位。
如果你實際上正如jewelsea所說的那樣正確地聲明你的ComboBox標識符(如果沒有,我認爲會出現其他錯誤)。

事實上,一切都很好的聲明(沒有語法錯誤或編譯錯誤)。
錯誤發生在運行時,事件@FXML protected void test(ActionEvent event)在您向ComboBox中填充/添加數據時正在執行。
由於沒有檢測到用戶輸入(我假設您在初始化場景時將數據添加到其他位置的ComboBox中),因此value屬性未更改。
所以getValue()返回null。

在這種情況下,線,其斷裂的代碼是:

System.out.println(output); 

因爲輸出是null

嘗試在test(ActionEvent event)方法的開始處放置斷點。

我希望這也有助於其他人。