FXML ComboBox,如何将选定的值传递给JavaFX?

15

我该如何获取FXML ComboBox的选定值并将其实现到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

提前致谢

僵尸

4个回答

33

试试这个:

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

这对我很有帮助!我正在寻找正确的方法来获取所选索引和项目,因为getValue在其他地方,而所选项目在这里。 - Pini Cheyni
Sample.getSelectionModel().getSelectedIndex() - QuadX

7
我认为只要代码中的combobox标识符与您的fxml中的fx:id匹配,那么您在问题中提供的代码应该可以正常工作。
我修改了这个JavaFX fxml combo box selection demonstration app,添加了一个带有onAction方法的按钮,使用comboBox的getValue()方法从combo box中检索值,并且对我很有效。
检查大小写,我注意到您说fx:id是sample,但是在您的代码中您使用了Sample - 大小写必须匹配,否则fxml加载器将无法正确注入节点到您的控制器中。
很难说您代码中的NullPointerException是否与您的combobox值检索问题相关,因为您没有说明TW_JAVAFX_Undecorator.ButtonController.pruefen(ButtonController.java:60)处的代码或提供完整可执行代码以复制该问题。

3
要获取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

我试图找到这个错误的答案(在同样的情况下刚刚发生在我身上),并找到了这篇文章。
如果您像jewelsea所说的那样正确声明了ComboBox标识符(如果没有,我认为其他错误也会出现)。

事实是一切都被正确声明了(没有语法错误或编译错误)。
错误在运行时发生,当您填充/添加数据到ComboBox时,事件@FXML protected void test(ActionEvent event)正在执行。
但是由于没有检测到用户输入(我假设您在初始化场景时在其他地方添加数据到ComboBox),因此值属性没有更改。
所以getValue()返回null。

在这种情况下,破坏代码的行是:

System.out.println(output);

因为输出是null

尝试在test(ActionEvent event)方法的开头设置断点。

我希望这也能帮助其他人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接