JavaFX and SceneBuilder -can't get the source of the event when it is a MenuItem










0















Good Morning I am working on a project but I have a problem. In fact I made MenuItems and Buttons with SceneBuilder and I have added a method "ouvrir" that should be activated when we click on the buttons or the menuItems. In fact, I take the id of the source of the event to open a tab. This method works for the buttons but not for the MenuItem. When I click on the MenuItem,it generates an error.



Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 43 more
Caused by: java.lang.ClassCastException: javafx.scene.control.MenuItem cannot be cast to javafx.scene.Node
at app_views.app_comptabilite.FXMLDocumentController.ouvrir(FXMLDocumentController.java:227)
... 53 more


This is the source code of the controller. The concerned method("ouvrir") is at the end of the code.



import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
public class FXMLDocumentController implements Initializable {

@FXML
private MenuBar barre_menu;

@FXML
private Menu menu_fichier;

@FXML
private MenuItem menu_nouvel_article;

@FXML
private MenuItem menu_charger;

@FXML
private MenuItem menu_enregistrer;

@FXML
private MenuItem menu_quitter;

@FXML
private Menu menu_action;

@FXML
private MenuItem action_vente;

@FXML
private MenuItem action_achat;

@FXML
private Menu menu_versement;

@FXML
private MenuItem versement_credit;

@FXML
private MenuItem versement_débit;

@FXML
private Menu menu_listes;

@FXML
private MenuItem liste_vente;

@FXML
private MenuItem liste_achat;

@FXML
private MenuItem liste_article;

@FXML
private MenuItem liste_produit;

@FXML
private MenuItem liste_crédit;

@FXML
private MenuItem liste_débit;

@FXML
private MenuItem liste_facture;

@FXML
private MenuItem liste_bon_de_commande;

@FXML
private Menu menu_compte;

@FXML
private Menu menu_utilisateur;

@FXML
private MenuItem liste_utilisateurs;


@FXML
private MenuItem ajouter_utilisateur;

@FXML
private Menu menu_aide;

@FXML
private MenuItem aide_dicdaticiel;

@FXML
private MenuItem aide_apropos;

@FXML
private Tab tab_accueil;

@FXML
private ToggleButton tool_accueil;
@FXML
private ToggleGroup groupe_toggle;
@FXML
private ToggleButton tool_nouvelArticle;
@FXML
private ToggleButton tool_liste_articles;
@FXML
private ToggleButton tool_liste_produits;
@FXML
private ToggleButton tool_achat;
@FXML
private ToggleButton tool_vente;
@FXML
private ToggleButton tool_bonDeCommande;
@FXML
private ToggleButton tool_credit;
@FXML
private ToggleButton tool_debit;
@FXML
private ToggleButton tool_liste_utilisateur;
@FXML
private ToggleButton tool_ajouter_utilisateur;
@FXML
private ToggleButton tool_calculatrice;
@FXML
private ToggleButton tool_aide;
@FXML
private ToggleButton tool_abonnement;

private final ArrayList<AnchorPane> contenuOnglets=new ArrayList<AnchorPane>();
@FXML
private ToggleGroup groupe_calc;
@FXML
private ToggleGroup groupe_aide;

@FXML
private ToggleGroup groupe_abonnement;

@FXML
private TabPane barreOnglets;
@FXML
private MenuItem solde;
@FXML
private MenuItem calculatrice;
@FXML
private MenuItem abonnement;

@Override
public void initialize(URL url, ResourceBundle rb)
initTab();


@FXML
private void initTab()
//La méthode sert à introduire le contenu des onglets dans une arraylist
/*for(Menu menu: barre_menu.getMenus())
for(MenuItem mi: menu.getItems())
*/
AnchorPane ap=new AnchorPane(),ap2=new AnchorPane();
ap.setId("Accueil");
ap2.setId("Créer un bon de commande");
contenuOnglets.add(ap);
contenuOnglets.add(ap2);

//Ne pas oublier les tools bon de commande et accueil
//Cet algo est utilisé de maniere temporaire
//Par la suite on créera les fxml des anchorpane correspondants
//en leur donnant les id correspondants


private void choisirOnglet(String id)
for(AnchorPane anchorPane: contenuOnglets)
if(anchorPane.getId().equals(id))
Tab tab= new Tab(id);
int valeur=0;
for(Tab t: barreOnglets.getTabs())
if(t.getText().equals(id))
valeur=1;
barreOnglets.getSelectionModel().select(t);
break;


if(valeur==0)
tab.setContent(anchorPane);
barreOnglets.getTabs().add(tab);
barreOnglets.getSelectionModel().select(tab);

break;




@FXML
private void ouvrir(Event event)
System.out.println("Fonction ouvrir");
//This is the line that generates the error
String id=((Node)event.getSource()).getId();
choisirOnglet(id);




Thanks for your help










share|improve this question






















  • onAction handlers for MenuItems have the MenuItem as source. MenuItem is not a subtype of Node which results in the exception.

    – fabian
    Nov 14 '18 at 20:21











  • thank you. I was thinking that all those menu, button.... were subtypes of Node. But please how can I get the id of MenuItem? Is there a parent of MenuItem and Button that has the getId() method? I want to factorize the code as much as possible

    – Esdras Fandio
    Nov 16 '18 at 4:05















0















Good Morning I am working on a project but I have a problem. In fact I made MenuItems and Buttons with SceneBuilder and I have added a method "ouvrir" that should be activated when we click on the buttons or the menuItems. In fact, I take the id of the source of the event to open a tab. This method works for the buttons but not for the MenuItem. When I click on the MenuItem,it generates an error.



Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 43 more
Caused by: java.lang.ClassCastException: javafx.scene.control.MenuItem cannot be cast to javafx.scene.Node
at app_views.app_comptabilite.FXMLDocumentController.ouvrir(FXMLDocumentController.java:227)
... 53 more


This is the source code of the controller. The concerned method("ouvrir") is at the end of the code.



import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
public class FXMLDocumentController implements Initializable {

@FXML
private MenuBar barre_menu;

@FXML
private Menu menu_fichier;

@FXML
private MenuItem menu_nouvel_article;

@FXML
private MenuItem menu_charger;

@FXML
private MenuItem menu_enregistrer;

@FXML
private MenuItem menu_quitter;

@FXML
private Menu menu_action;

@FXML
private MenuItem action_vente;

@FXML
private MenuItem action_achat;

@FXML
private Menu menu_versement;

@FXML
private MenuItem versement_credit;

@FXML
private MenuItem versement_débit;

@FXML
private Menu menu_listes;

@FXML
private MenuItem liste_vente;

@FXML
private MenuItem liste_achat;

@FXML
private MenuItem liste_article;

@FXML
private MenuItem liste_produit;

@FXML
private MenuItem liste_crédit;

@FXML
private MenuItem liste_débit;

@FXML
private MenuItem liste_facture;

@FXML
private MenuItem liste_bon_de_commande;

@FXML
private Menu menu_compte;

@FXML
private Menu menu_utilisateur;

@FXML
private MenuItem liste_utilisateurs;


@FXML
private MenuItem ajouter_utilisateur;

@FXML
private Menu menu_aide;

@FXML
private MenuItem aide_dicdaticiel;

@FXML
private MenuItem aide_apropos;

@FXML
private Tab tab_accueil;

@FXML
private ToggleButton tool_accueil;
@FXML
private ToggleGroup groupe_toggle;
@FXML
private ToggleButton tool_nouvelArticle;
@FXML
private ToggleButton tool_liste_articles;
@FXML
private ToggleButton tool_liste_produits;
@FXML
private ToggleButton tool_achat;
@FXML
private ToggleButton tool_vente;
@FXML
private ToggleButton tool_bonDeCommande;
@FXML
private ToggleButton tool_credit;
@FXML
private ToggleButton tool_debit;
@FXML
private ToggleButton tool_liste_utilisateur;
@FXML
private ToggleButton tool_ajouter_utilisateur;
@FXML
private ToggleButton tool_calculatrice;
@FXML
private ToggleButton tool_aide;
@FXML
private ToggleButton tool_abonnement;

private final ArrayList<AnchorPane> contenuOnglets=new ArrayList<AnchorPane>();
@FXML
private ToggleGroup groupe_calc;
@FXML
private ToggleGroup groupe_aide;

@FXML
private ToggleGroup groupe_abonnement;

@FXML
private TabPane barreOnglets;
@FXML
private MenuItem solde;
@FXML
private MenuItem calculatrice;
@FXML
private MenuItem abonnement;

@Override
public void initialize(URL url, ResourceBundle rb)
initTab();


@FXML
private void initTab()
//La méthode sert à introduire le contenu des onglets dans une arraylist
/*for(Menu menu: barre_menu.getMenus())
for(MenuItem mi: menu.getItems())
*/
AnchorPane ap=new AnchorPane(),ap2=new AnchorPane();
ap.setId("Accueil");
ap2.setId("Créer un bon de commande");
contenuOnglets.add(ap);
contenuOnglets.add(ap2);

//Ne pas oublier les tools bon de commande et accueil
//Cet algo est utilisé de maniere temporaire
//Par la suite on créera les fxml des anchorpane correspondants
//en leur donnant les id correspondants


private void choisirOnglet(String id)
for(AnchorPane anchorPane: contenuOnglets)
if(anchorPane.getId().equals(id))
Tab tab= new Tab(id);
int valeur=0;
for(Tab t: barreOnglets.getTabs())
if(t.getText().equals(id))
valeur=1;
barreOnglets.getSelectionModel().select(t);
break;


if(valeur==0)
tab.setContent(anchorPane);
barreOnglets.getTabs().add(tab);
barreOnglets.getSelectionModel().select(tab);

break;




@FXML
private void ouvrir(Event event)
System.out.println("Fonction ouvrir");
//This is the line that generates the error
String id=((Node)event.getSource()).getId();
choisirOnglet(id);




Thanks for your help










share|improve this question






















  • onAction handlers for MenuItems have the MenuItem as source. MenuItem is not a subtype of Node which results in the exception.

    – fabian
    Nov 14 '18 at 20:21











  • thank you. I was thinking that all those menu, button.... were subtypes of Node. But please how can I get the id of MenuItem? Is there a parent of MenuItem and Button that has the getId() method? I want to factorize the code as much as possible

    – Esdras Fandio
    Nov 16 '18 at 4:05













0












0








0








Good Morning I am working on a project but I have a problem. In fact I made MenuItems and Buttons with SceneBuilder and I have added a method "ouvrir" that should be activated when we click on the buttons or the menuItems. In fact, I take the id of the source of the event to open a tab. This method works for the buttons but not for the MenuItem. When I click on the MenuItem,it generates an error.



Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 43 more
Caused by: java.lang.ClassCastException: javafx.scene.control.MenuItem cannot be cast to javafx.scene.Node
at app_views.app_comptabilite.FXMLDocumentController.ouvrir(FXMLDocumentController.java:227)
... 53 more


This is the source code of the controller. The concerned method("ouvrir") is at the end of the code.



import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
public class FXMLDocumentController implements Initializable {

@FXML
private MenuBar barre_menu;

@FXML
private Menu menu_fichier;

@FXML
private MenuItem menu_nouvel_article;

@FXML
private MenuItem menu_charger;

@FXML
private MenuItem menu_enregistrer;

@FXML
private MenuItem menu_quitter;

@FXML
private Menu menu_action;

@FXML
private MenuItem action_vente;

@FXML
private MenuItem action_achat;

@FXML
private Menu menu_versement;

@FXML
private MenuItem versement_credit;

@FXML
private MenuItem versement_débit;

@FXML
private Menu menu_listes;

@FXML
private MenuItem liste_vente;

@FXML
private MenuItem liste_achat;

@FXML
private MenuItem liste_article;

@FXML
private MenuItem liste_produit;

@FXML
private MenuItem liste_crédit;

@FXML
private MenuItem liste_débit;

@FXML
private MenuItem liste_facture;

@FXML
private MenuItem liste_bon_de_commande;

@FXML
private Menu menu_compte;

@FXML
private Menu menu_utilisateur;

@FXML
private MenuItem liste_utilisateurs;


@FXML
private MenuItem ajouter_utilisateur;

@FXML
private Menu menu_aide;

@FXML
private MenuItem aide_dicdaticiel;

@FXML
private MenuItem aide_apropos;

@FXML
private Tab tab_accueil;

@FXML
private ToggleButton tool_accueil;
@FXML
private ToggleGroup groupe_toggle;
@FXML
private ToggleButton tool_nouvelArticle;
@FXML
private ToggleButton tool_liste_articles;
@FXML
private ToggleButton tool_liste_produits;
@FXML
private ToggleButton tool_achat;
@FXML
private ToggleButton tool_vente;
@FXML
private ToggleButton tool_bonDeCommande;
@FXML
private ToggleButton tool_credit;
@FXML
private ToggleButton tool_debit;
@FXML
private ToggleButton tool_liste_utilisateur;
@FXML
private ToggleButton tool_ajouter_utilisateur;
@FXML
private ToggleButton tool_calculatrice;
@FXML
private ToggleButton tool_aide;
@FXML
private ToggleButton tool_abonnement;

private final ArrayList<AnchorPane> contenuOnglets=new ArrayList<AnchorPane>();
@FXML
private ToggleGroup groupe_calc;
@FXML
private ToggleGroup groupe_aide;

@FXML
private ToggleGroup groupe_abonnement;

@FXML
private TabPane barreOnglets;
@FXML
private MenuItem solde;
@FXML
private MenuItem calculatrice;
@FXML
private MenuItem abonnement;

@Override
public void initialize(URL url, ResourceBundle rb)
initTab();


@FXML
private void initTab()
//La méthode sert à introduire le contenu des onglets dans une arraylist
/*for(Menu menu: barre_menu.getMenus())
for(MenuItem mi: menu.getItems())
*/
AnchorPane ap=new AnchorPane(),ap2=new AnchorPane();
ap.setId("Accueil");
ap2.setId("Créer un bon de commande");
contenuOnglets.add(ap);
contenuOnglets.add(ap2);

//Ne pas oublier les tools bon de commande et accueil
//Cet algo est utilisé de maniere temporaire
//Par la suite on créera les fxml des anchorpane correspondants
//en leur donnant les id correspondants


private void choisirOnglet(String id)
for(AnchorPane anchorPane: contenuOnglets)
if(anchorPane.getId().equals(id))
Tab tab= new Tab(id);
int valeur=0;
for(Tab t: barreOnglets.getTabs())
if(t.getText().equals(id))
valeur=1;
barreOnglets.getSelectionModel().select(t);
break;


if(valeur==0)
tab.setContent(anchorPane);
barreOnglets.getTabs().add(tab);
barreOnglets.getSelectionModel().select(tab);

break;




@FXML
private void ouvrir(Event event)
System.out.println("Fonction ouvrir");
//This is the line that generates the error
String id=((Node)event.getSource()).getId();
choisirOnglet(id);




Thanks for your help










share|improve this question














Good Morning I am working on a project but I have a problem. In fact I made MenuItems and Buttons with SceneBuilder and I have added a method "ouvrir" that should be activated when we click on the buttons or the menuItems. In fact, I take the id of the source of the event to open a tab. This method works for the buttons but not for the MenuItem. When I click on the MenuItem,it generates an error.



Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 43 more
Caused by: java.lang.ClassCastException: javafx.scene.control.MenuItem cannot be cast to javafx.scene.Node
at app_views.app_comptabilite.FXMLDocumentController.ouvrir(FXMLDocumentController.java:227)
... 53 more


This is the source code of the controller. The concerned method("ouvrir") is at the end of the code.



import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
public class FXMLDocumentController implements Initializable {

@FXML
private MenuBar barre_menu;

@FXML
private Menu menu_fichier;

@FXML
private MenuItem menu_nouvel_article;

@FXML
private MenuItem menu_charger;

@FXML
private MenuItem menu_enregistrer;

@FXML
private MenuItem menu_quitter;

@FXML
private Menu menu_action;

@FXML
private MenuItem action_vente;

@FXML
private MenuItem action_achat;

@FXML
private Menu menu_versement;

@FXML
private MenuItem versement_credit;

@FXML
private MenuItem versement_débit;

@FXML
private Menu menu_listes;

@FXML
private MenuItem liste_vente;

@FXML
private MenuItem liste_achat;

@FXML
private MenuItem liste_article;

@FXML
private MenuItem liste_produit;

@FXML
private MenuItem liste_crédit;

@FXML
private MenuItem liste_débit;

@FXML
private MenuItem liste_facture;

@FXML
private MenuItem liste_bon_de_commande;

@FXML
private Menu menu_compte;

@FXML
private Menu menu_utilisateur;

@FXML
private MenuItem liste_utilisateurs;


@FXML
private MenuItem ajouter_utilisateur;

@FXML
private Menu menu_aide;

@FXML
private MenuItem aide_dicdaticiel;

@FXML
private MenuItem aide_apropos;

@FXML
private Tab tab_accueil;

@FXML
private ToggleButton tool_accueil;
@FXML
private ToggleGroup groupe_toggle;
@FXML
private ToggleButton tool_nouvelArticle;
@FXML
private ToggleButton tool_liste_articles;
@FXML
private ToggleButton tool_liste_produits;
@FXML
private ToggleButton tool_achat;
@FXML
private ToggleButton tool_vente;
@FXML
private ToggleButton tool_bonDeCommande;
@FXML
private ToggleButton tool_credit;
@FXML
private ToggleButton tool_debit;
@FXML
private ToggleButton tool_liste_utilisateur;
@FXML
private ToggleButton tool_ajouter_utilisateur;
@FXML
private ToggleButton tool_calculatrice;
@FXML
private ToggleButton tool_aide;
@FXML
private ToggleButton tool_abonnement;

private final ArrayList<AnchorPane> contenuOnglets=new ArrayList<AnchorPane>();
@FXML
private ToggleGroup groupe_calc;
@FXML
private ToggleGroup groupe_aide;

@FXML
private ToggleGroup groupe_abonnement;

@FXML
private TabPane barreOnglets;
@FXML
private MenuItem solde;
@FXML
private MenuItem calculatrice;
@FXML
private MenuItem abonnement;

@Override
public void initialize(URL url, ResourceBundle rb)
initTab();


@FXML
private void initTab()
//La méthode sert à introduire le contenu des onglets dans une arraylist
/*for(Menu menu: barre_menu.getMenus())
for(MenuItem mi: menu.getItems())
*/
AnchorPane ap=new AnchorPane(),ap2=new AnchorPane();
ap.setId("Accueil");
ap2.setId("Créer un bon de commande");
contenuOnglets.add(ap);
contenuOnglets.add(ap2);

//Ne pas oublier les tools bon de commande et accueil
//Cet algo est utilisé de maniere temporaire
//Par la suite on créera les fxml des anchorpane correspondants
//en leur donnant les id correspondants


private void choisirOnglet(String id)
for(AnchorPane anchorPane: contenuOnglets)
if(anchorPane.getId().equals(id))
Tab tab= new Tab(id);
int valeur=0;
for(Tab t: barreOnglets.getTabs())
if(t.getText().equals(id))
valeur=1;
barreOnglets.getSelectionModel().select(t);
break;


if(valeur==0)
tab.setContent(anchorPane);
barreOnglets.getTabs().add(tab);
barreOnglets.getSelectionModel().select(tab);

break;




@FXML
private void ouvrir(Event event)
System.out.println("Fonction ouvrir");
//This is the line that generates the error
String id=((Node)event.getSource()).getId();
choisirOnglet(id);




Thanks for your help







java javafx-8 scenebuilder






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 18:23









Esdras FandioEsdras Fandio

11




11












  • onAction handlers for MenuItems have the MenuItem as source. MenuItem is not a subtype of Node which results in the exception.

    – fabian
    Nov 14 '18 at 20:21











  • thank you. I was thinking that all those menu, button.... were subtypes of Node. But please how can I get the id of MenuItem? Is there a parent of MenuItem and Button that has the getId() method? I want to factorize the code as much as possible

    – Esdras Fandio
    Nov 16 '18 at 4:05

















  • onAction handlers for MenuItems have the MenuItem as source. MenuItem is not a subtype of Node which results in the exception.

    – fabian
    Nov 14 '18 at 20:21











  • thank you. I was thinking that all those menu, button.... were subtypes of Node. But please how can I get the id of MenuItem? Is there a parent of MenuItem and Button that has the getId() method? I want to factorize the code as much as possible

    – Esdras Fandio
    Nov 16 '18 at 4:05
















onAction handlers for MenuItems have the MenuItem as source. MenuItem is not a subtype of Node which results in the exception.

– fabian
Nov 14 '18 at 20:21





onAction handlers for MenuItems have the MenuItem as source. MenuItem is not a subtype of Node which results in the exception.

– fabian
Nov 14 '18 at 20:21













thank you. I was thinking that all those menu, button.... were subtypes of Node. But please how can I get the id of MenuItem? Is there a parent of MenuItem and Button that has the getId() method? I want to factorize the code as much as possible

– Esdras Fandio
Nov 16 '18 at 4:05





thank you. I was thinking that all those menu, button.... were subtypes of Node. But please how can I get the id of MenuItem? Is there a parent of MenuItem and Button that has the getId() method? I want to factorize the code as much as possible

– Esdras Fandio
Nov 16 '18 at 4:05












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306552%2fjavafx-and-scenebuilder-cant-get-the-source-of-the-event-when-it-is-a-menuitem%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306552%2fjavafx-and-scenebuilder-cant-get-the-source-of-the-event-when-it-is-a-menuitem%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto