Fxml combobox, get selected value in javafx

How can I catch the selected fxml combo box value and implement it in the javafx class?

I gave combobox fx: id "sample" and created a button with onAction = "# test" and tried .getValue and .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); } 

If I try to start it, I get an error message:

 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 

Thanks in advance

zombie

+7
source share
4 answers

I think the code that you have in your question should work as long as the combobox identifier case in the code matches the code of your fxml fx:id .

I modified this JavaFX fxml application to demonstrate the choice of the box socket to add a button with the onAction method to extract the value from the combo box using the comboBox getValue() method and it worked for me.

Check the situation, I noticed that you said fx:id sample , but in your code you use sample - and the cases should match, otherwise the fxml loader will not enter node into your controller correctly.

It is difficult to say that if a NullPointerException in your code is related to the problem of getting the value of the cell field, because you do not say what the code is in TW_JAVAFX_Undecorator.ButtonController.pruefen(ButtonController.java:60) , or provide the full executable code to replicate the problem.

+6
source

Try the following:

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

To get the selected ComboBox value, you can use the Sample.getSelectionModel method.

Example:

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

I tried to find the answer to this error (which just happened to me under the same conditions) and found this post. If you really provided your ComboBox ID correctly, as jewelsea said (if not all the same, I think another error would have appeared).

The fact is that everything was well declared (no syntax error or error compilation).
Runtime error, the @FXML protected void test(ActionEvent event) is executed when filling / adding data to the ComboBox.
But the property does not change as user input has not been detected (I assume that you add data to your ComboBox somewhere else when you initialize the scene).
Therefore, getValue() returns null.

In this case, the line that broke the code:

 System.out.println(output); 

Since the output is null .

Try test(ActionEvent event) breakpoint at the beginning of the test(ActionEvent event) method.

I expect this help to help others too.

0
source

All Articles