14

如何捕获 fxml 组合框的选定值并将其实现到 javafx 类中?

我给了组合框 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

4 回答 4

33

试试这个:

String output = Sample.getSelectionModel().getSelectedItem();
System.out.println(output);
于 2013-07-26T00:00:16.747 回答
7

我认为只要代码中组合框标识符的大小写与 fxml 的大小写匹配,您在问题中的代码就应该可以工作fx:id

我修改了这个JavaFX fxml 组合框选择演示应用程序,添加了一个带有 onAction 方法的按钮,以使用 comboBoxgetValue()方法从组合框中检索值,它对我来说效果很好。

检查事物的大小写,我注意到您说fx:idis sample,但在您使用的代码中Sample- 大小写必须匹配,否则 fxml 加载器将无法将节点正确注入您的控制器。

很难说NullPointerException您的代码中的 是否与您的组合框值检索问题有关,因为您没有说明代码TW_JAVAFX_Undecorator.ButtonController.pruefen(ButtonController.java:60)是什么或提供完整的可执行代码来复制问题。

于 2013-05-09T06:37:25.610 回答
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);
        }
});
于 2014-03-22T04:06:52.227 回答
0

我试图找到这个错误的答案(这只是在相同的条件下发生在我身上)并找到了这篇文章。
如果你真的像珠宝海所说的那样正确地声明了你的 ComboBox 标识符(如果不是这样,我认为会出现其他错误)。

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

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

System.out.println(output);

因为输出是null.

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

我希望这对其他人也有帮助。

于 2014-05-12T16:19:58.810 回答