2013-05-08 7 views
7

Comment puis-je attraper la valeur sélectionnée d'une combobox fxml et l'implémenter dans une classe javafx?fxml combobox, obtenez la valeur sélectionnée dans javafx

J'ai donné à la combobox le fx: id "sample" et créé un bouton avec onAction = "# test" et essayé .getValue et .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); 
} 

Si je tente de l'exécuter j'obtiens une erreur:

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 

Merci à l'avance

Zombie

Répondre

6

Je pense que le code que vous avez dans votre question devrait travailler aussi longtemps comme le cas de l'identifiant combobox dans le code correspond à celui de votre fxml fx:id.

J'ai modifié ce JavaFX fxml combo box selection demonstration app pour ajouter un bouton avec une méthode onAction pour récupérer une valeur de la zone de liste déroulante en utilisant la méthode comboBox getValue() et cela a bien fonctionné pour moi.

Vérifiez le cas des choses, je remarque que vous dites le fx:id est sample, mais dans votre code que vous utilisez Sample - et les cas doivent correspondre autrement le chargeur de FXML ne sera pas injecter correctement le nœud dans votre contrôleur. Il est difficile de dire si le NullPointerException dans votre code est lié à votre problème de récupération de valeur de zone de liste déroulante car vous ne dites pas ce que le code à TW_JAVAFX_Undecorator.ButtonController.pruefen(ButtonController.java:60) est ou fournir un code exécutable complet pour répliquer le problème.

25

Essayez ceci:

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

Cela m'a aidé! Je cherchais la bonne méthode pour obtenir l'index et l'élément sélectionnés, car getValue est ailleurs et l'élément sélectionné est ici. –

+0

Sample.getSelectionModel(). GetSelectedIndex() – QuadX

+3

La diffusion est redondante. toString() est toujours de type String. – Zon

2

Pour obtenir la valeur sélectionnée ComboBox, vous pouvez utiliser la méthode Sample.getSelectionModel.

Exemple:

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

merci cela a fonctionné pour moi. –

0

je tentais de trouver une réponse à cette erreur (qui vient de se passer pour moi dans les conditions mêmes) et a trouvé ce poste.
Si vous avez effectivement déclaré votre identifiant ComboBox correctement comme je l'ai dit jewelsea (Si ce n'est pas le cas je pense qu'une autre erreur serait apparue). Le fait est que tout a été bien déclaré (pas d'erreur de syntaxe ou erreur de compilation).
L'erreur est en cours d'exécution, l'événement @FXML protected void test(ActionEvent event) est en cours d'exécution lorsque vous remplissez/ajoutez des données à votre zone de liste déroulante.
Mais la propriété de valeur ne change pas car aucune entrée utilisateur n'a été détectée (je suppose que vous ajoutez des données à votre ComboBox quelque part lorsque vous initialisez la scène).
Donc, getValue() renvoie null.

Dans ce cas, la ligne qui cassé le code est:

System.out.println(output); 

Parce que la sortie est null.Essayez de mettre un point d'arrêt dans la méthode test(ActionEvent event).

J'attends cette aide aussi d'autres.

Questions connexes