0

i have a simple JavaFx browser that is launched through a JButton that it's placed into a JFrame. Now, i wanted to put the JavaFx broser into a separate thread that should run even if the JFrame is closed. Now, if i close the JFrame with that JButton, the JavaFx browser also closes. I've tried multiple combinations, done research over internet, but with no success. Hope you guys can help me with a little example. Thanks !

 browser.addActionListener(new ActionListener() {
                         public void actionPerformed(ActionEvent e) {
               if(e.getSource() == browser) {
                   
                
                   
                     
                     // create a JFXPanel, which will start the FX toolkit
                // if it's not already started: - folosit pt a integra componente de tip FX Stage in componente swing (mai vechi) (JFrame)
                JFXPanel fxPanel = new JFXPanel();
                
                 Platform.runLater(new Runnable() {
                     
                     
@Override
                    public void run() {
                       
                        Scene scene;
                        TextField addressField;
                        WebView webView;
                        WebEngine webEngine;
                        Stage stage = null;
                        Button reloadButton, goButton, backButton , forwardButton, historyList;
                      
                       
          
                        
                        HBox hBox = new HBox(5);
                        hBox.setAlignment(Pos.CENTER);
                        //The TextField for entering web addresses.
                        addressField = new TextField("Ionutz says: Enter the address here....");
                        addressField.setPrefColumnCount(50); //make the field at least 50 columns wide.
                        //Add all out navigation nodes to the vbox.
                        reloadButton = new Button("Reload page");
                        goButton = new Button("Search");
                        
                        
                        backButton = new Button("Back");
                        forwardButton = new Button("Forward");  
                        historyList = new Button("History");
                        
                        String urlSearch = "http://icons.iconarchive.com/icons/ampeross/qetto-2/24/search-icon.png";
                        String urlReload = "http://icons.iconarchive.com/icons/graphicloads/colorful-long-shadow/24/Arrow-reload-2-icon.png";
                        
                        String URLBack = "http://icons.iconarchive.com/icons/custom-icon-design/flatastic-1/24/back-icon.png";
    String URLForward = "http://icons.iconarchive.com/icons/custom-icon-design/flatastic-1/24/forward-icon.png";
    String URLHistory = "http://icons.iconarchive.com/icons/delacro/id/24/History-icon.png";
    
    goButton.setGraphic(new ImageView(urlSearch));
    
    reloadButton.setGraphic(new ImageView(urlReload));
        
    forwardButton.setGraphic(new ImageView(URLForward));
    
    backButton.setGraphic(new ImageView(URLBack));
    
    historyList.setGraphic(new ImageView(URLHistory));
                        
                        
                        hBox.getChildren().addAll(backButton, forwardButton, addressField, reloadButton, goButton, historyList);
                        //Our weiv that display ther page.
                        webView = new WebView();
                        //the engine that manages our pages.
                        webEngine = webView.getEngine();
                        webEngine.setJavaScriptEnabled(true);
                        webEngine.load("http://www.google.ro");
                        //our main app layout with 5 regions.
                        BorderPane root = new BorderPane();
                        root.setPrefSize(1280, 720);
                        //Add every node to the BorderPane.
                        root.setTop(hBox);
                        root.setCenter(webView);
                        //Our scene is where all the action in JavaFX happens.  A scene holds all Nodes, and its root node is our BorderPane.
                        scene = new Scene(root);
                        //the stage manages the scene.
                        fxPanel.setScene(scene);
                        JFrame browserFrame = new JFrame();
                        browserFrame.add(fxPanel);
                        browserFrame.setTitle("Ionutz Asaftei Browser");
                        browserFrame.setBounds(200, 200, 1280, 720);
                        browserFrame.setVisible(true);
                      
                        
                    
                          
                              // adaugam event handlers !!!! - cele mai simple pt Buttons
        reloadButton.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
            public void handle(javafx.event.ActionEvent event) {
              webEngine.reload();
              
            }
        });
        
        goButton.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
            public void handle(javafx.event.ActionEvent event) {
                webEngine.load("http://"+addressField.getText());
            }
        });
        
         
                         
        forwardButton.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
            public void handle(javafx.event.ActionEvent event) {
                            webEngine.getHistory().go(1); //avanseaza cu o pagina in functie de intrarile din history
                
                        }
                 });
        
        backButton.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
            public void handle(javafx.event.ActionEvent ev) {
               webEngine.getHistory().go(-1); //merge in urma cu o pagina in functie de intrarile din history
            }
        });
                }
                    
                 });
                   
                  
                           }
                         }
                        
                     });

1 Answers1

0

The JavaFX toolkit is single threaded from a user application point of view. It is not possible to spawn multiple threads launching WebView instances.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Why this limitation ? :( If this is the only way, i will make the browser separate from main application. :( – Ionutz Asaftei Sep 28 '15 at 13:58
  • To understand the reason for single-threadeding rules in UI toolkits see some of the commentary in the answer to [Javafx: Difference between javafx.concurent and Platform.runLater?](http://stackoverflow.com/questions/18710039/javafx-difference-between-javafx-concurent-and-platform-runlater). – jewelsea Sep 28 '15 at 16:37