Using JavaFX's MenuBar causes 'Glass detected outstanding Java exception' -
i have javafx application. have button when clicked calls action , loads fxml file. works perfectly.
i decided place functionality within menu of application.
so, created menu , added menu items within scene builder. assign 'on action' event, same way did other button. following error when click:
glass detected outstanding java exception @ -[glassviewdelegate sendjavamouseevent:]:src/com/sun/mat/ui/glassviewdelegate.m:541 exception in thread "javafx application thread" java.lang.runtimeexception: java.lang.reflect.invocationtargetexception caused by: java.lang.classcastexception: javafx.scene.control.menuitem cannot cast javafx.scene.node @ plataformavalidezpredictiva.maincontroller.handleaction(maincontroller.java:60) ... 38 more this code handler. once again, works button placed within ui , doesn't work menu bar:
public void handleaction(actionevent event) throws exception{ node node = (node)event.getsource(); stage stage=(stage) node.getscene().getwindow(); fxmlloader fxmlloader = new fxmlloader(getclass().getresource("fxml/guifile.fxml")); parent root = (parent)fxmlloader.load(); scene scene = new scene(root); stage.setscene(scene); stage.show(); } the line seems give me problem is:
node node = (node)event.getsource(); any ideas?
edit: saw post here unable scene menuitem in javafx didn't work me, because didn't find getscene() method menu bar.
inject node (e.g. menubar, can node in same scene) controller. call getscene() on node , getwindow() on scene.
e.g.
main.fxml:
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.borderpane?> <?import javafx.scene.control.menubar?> <?import javafx.scene.control.menu?> <?import javafx.scene.control.menuitem?> <borderpane xmlns:fx="http://javafx.com/fxml" fx:controller="exitfrommenu.maincontroller"> <top> <menubar fx:id="menubar"> <menu text="file"> <menuitem text="exit" onaction="#exit"/> </menu> </menubar> </top> </borderpane> maincontroller.java
package exitfrommenu; import javafx.fxml.fxml; import javafx.scene.control.menubar; public class maincontroller { @fxml private menubar menubar ; @fxml private void exit() { stage stage = (stage) menubar.getscene().getwindow() ; // exits application, of course can // stage, such showing new scene in it: stage.hide(); } } main.java
package exitfrommenu; import javafx.application.application; import javafx.stage.stage; import javafx.scene.scene; import javafx.scene.layout.borderpane; import javafx.fxml.fxmlloader; public class main extends application { @override public void start(stage primarystage) { try { borderpane root = (borderpane)fxmlloader.load(getclass().getresource("main.fxml")); scene scene = new scene(root,400,400); primarystage.setscene(scene); primarystage.show(); } catch(exception e) { e.printstacktrace(); } } public static void main(string[] args) { launch(args); } }
Comments
Post a Comment