ReedWilliams19842004
ReedWilliams19842004

Reputation: 65

set a java application to close when a JavaFX browser window is closed?

How do I set a java application to close when a JavaFX browser window is closed?

I have a console based application, at the end of the main content, the user is is asked if they would like to view a web page. If yes, open the web page. If not exit app.

As I understand it, a javaFX app closes when the window is Xed out. My issue is, the part of my app that is not FX and is command/text does not appear to terminate when window closes. How can I achieve total app termination when FX window closes.

I will include two classes. One prompts the user to view page and in input n, then terminate. The other class opens the page.

package mrArray;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/*
 * declare class, subclass of Application.
 * declare, instantiate, initialize vBoxOF,
 */
public class OpenSite extends Application 
{
	VBox vBoxOF = new VBox();

	/*
	 * declare method.
	 * invoke OpenSite.launch(OpenSite.class);,
	 * launch a stand-alone application.
	 */
	public static void invokeLaunch() 
	{
		OpenSite.launch(OpenSite.class);
	}
	
	/*
	 * declare start(Stage primaryStage),
	 * main entry point for all JavaFX applications,
	 * declare primaryStageOP,
	 * the top level JavaFX container.
	 * invoke setId("root"),
	 * assign identifier.
	 * declare, instantiate, initialize webViewBrowserOL,
	 * as node that manages a webEngineOL & displays its content.
	 * 
	 *  managing one Web page 
	 */
	public void start(Stage primaryStage) 
	{
		vBoxOF.setId("root");

		WebView  webViewBrowserOL = new WebView();
		WebEngine webEngineOL = webViewBrowserOL.getEngine();
		String urlSL = "http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html";
		webEngineOL.load(urlSL);
    
		vBoxOF.setPadding(new Insets(30, 50, 50, 50));
		vBoxOF.setSpacing(10);
		vBoxOF.setAlignment(Pos.CENTER);
		vBoxOF.getChildren().addAll(webViewBrowserOL);

		Scene sceneOL = new Scene(vBoxOF);
		primaryStage.setScene(sceneOL);
		primaryStage.show();
		
		//ViewSiteOrExit.exitApp();
	}
}

package mrArray;


public class ViewSiteOrExit 
{
	/*
	 * declare fields,
	 */
	private static int statePrSIF;
	private static String enterYOrNPrSSOF;
	
	/*
	 * declare method,
	 * initialize field,
	 * while, test(field) is passing execute,
	 * switch, evaluates cases with value(field),
	 * matching, execute statements,
	 * 0, first case, prompt, y drop to if, reset value, use app again,
	 * n drop to else, increment field, 1, second case,
	 * invoke method to close app, reset field value to prevent double field invocation,
	 * return flow to caller to prevent use of closing Scanner,
	 */
	 public static void viewSitePromptPuSVM() 
	 {
		 statePrSIF = 0;
		 while (statePrSIF < 2) 
		 {
			 switch (statePrSIF) 
			 {
			 	case 0: 
			 		System.out.println();
			 		System.out.println("[:-)] One more question?");
			 		System.out.println("Would you like to see what Oracle has to say about me?");
			 		System.out.println("Enter ' y ' for yes.");
			 		System.out.println("Enter ' n ' for no.");
			 		break;
		    	case 1:
		    		goodByePuSVM();
		    		statePrSIF = 0;
		    		return;
			 }
		          	 
			 enterYOrNPrSSOF = MrArray.scannerOF.next();
		    
			 if(enterYOrNPrSSOF.equalsIgnoreCase("y")) 
			 {
				 statePrSIF = 0;
				 System.out.println("[:-)] Well bud, it's been fun.");
				 System.out.println("Here is that Orcale thing.");
				 System.out.println("See ya later!");
				 OpenSite.invokeLaunch();
			 }
			 else if(enterYOrNPrSSOF.equalsIgnoreCase("n")) 
			 {
				 statePrSIF++;
			 }	
		 }
	 }
	 
	 /*
	  * declare method,
	  * invoke methods, display output,
	  * close Scanner, terminate,
	  */
	 public static void goodByePuSVM()
	 {
	    	System.out.println("[:-)] Well bud, it's been fun.");
	    	System.out.println("See ya later!");
	    	
	    	MrArray.scannerOF.close();
	    	exitApp();
	 }
	 
	 public static void exitApp()
	 {
		 System.exit(0); 
	 }
}

Upvotes: 0

Views: 1571

Answers (2)

Robert Martin
Robert Martin

Reputation: 1605

You should be using the Lambda Expression:

primaryStage.setOnCloseRequest(
    (WindowEvent we) -> {
        Platform.exit();
    });

Upvotes: 0

ItachiUchiha
ItachiUchiha

Reputation: 36722

If you have nothing to save, you can use System.exit(0) on stage.setOnCloseRequest() request.

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
        @Override
        public void handle(WindowEvent event) {
            System.exit(0);
        }
});

StreamLined Approach

Since you are calling JavaFX Application from your Java Application and once you close the JavaFX Application Window, the control will return to the Java Application, I would recommend the following approach.

In JavaFX Application Class

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {
        Platform.exit();
    }
});

In Java Class

OpenSite.invokeLaunch();
...        // Complete your task
if(!Platform.isFxApplicationThread())
      exitApp(); // or System.exit(0);

Upvotes: 0

Related Questions