java - throw Exception in lambda(or equivalent) - javafx -
i trying popup come asking user if wish exit program everytime click exit button (x in the top left corner). using fxml , have throw exception when try load popup primarystage.setoncloserequest(new eventhandler<windowevent>{}
keep getting unhandled exception error when throw error in method calling from. have tried try , catch , while there isn't error code doesn't run. trying see if can wrap method in can maybe around exception nothing has seemed work. there way use setoncloserequest
thrown exception
?
public class main extends application { @override public void start(stage primarystage)throws exception { file f = new file("goal.txt"); boolean bool = false; if (f.exists() ) { fxmlloader loader = new fxmlloader(getclass().getresource("mainpage.fxml")); parent root = loader.load(); primarystage.settitle("money saver program"); primarystage.setscene(new scene(root, 600, 400)); primarystage.show(); } else { bool = f.createnewfile(); parent root = fxmlloader.load(getclass().getresource("openingpage.fxml")); primarystage.settitle("money saver program"); primarystage.setscene(new scene(root, 638, 400)); primarystage.show(); } primarystage.setoncloserequest(new eventhandler<windowevent>() { public void handle(windowevent event) { event.consume(); closeprogram(); } }); } public static void main(string[] args) throws exception { launch(args); } public void closeprogram(){ try{internalcloseprogram();}catch (exception e){} } public void internalcloseprogram()throws exception{ fxmlloader fxmlloader = new fxmlloader(getclass().getresource("finalexitmessage.fxml")); parent root = fxmlloader.load(); stage stage = new stage(); stage.setscene(new scene(root)); stage.settitle("money saver program"); stage.show(); } }
you cannot throw checked exception lambda expression/anonymus class, if exception not assignable exception in throws
clause of abstract method of functional interface.
what done throwing new unchecked exception has exception it's cause:
try{ internalcloseprogram(); } catch (exception e){ throw new illegalstateexception("something went wrong", e); }
however nothing happening seems indication loading finalexitmessage.fxml
failed. note: "swallowing" exception not idea, unless know sure can safely ignored , don't need information debugging.
furthermore consuming event in oncloserequest
call, tell javafx ignore attempt close window. need close stage yourself, in case want close it, using
primarystage.close();
you use
platform.exit();
from stage
shown in internalcloseprogram
method.
also wait user interact new stage calling showandwait
instead of show
on new stage wait until new stage closed.
additional recommendation: use modality block events primary stage while new stage shown.
the following example asks, if window should closed using alert
. showandwait
method of alert
different stage.showandwait
: returns optional<buttontype>
returning user pick. stage
you'd need access controller result.
@override public void start(stage primarystage) { scene scene = new scene(new group(), 100, 100); primarystage.setoncloserequest(evt -> { alert alert = new alert(alert.alerttype.confirmation, "do want close applicetion?", buttontype.yes, buttontype.no); buttontype result = alert.showandwait().orelse(buttontype.no); if (buttontype.no.equals(result)) { // no choice or no clicked -> don't close evt.consume(); } }); primarystage.setscene(scene); primarystage.show(); }
Comments
Post a Comment