Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Clicking on menu item "open in new tab" on webview does not open webpage sometimes, other time it does open

  1. #1
    Junior Member
    Join Date
    Nov 2023
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Clicking on menu item "open in new tab" on webview does not open webpage sometimes, other time it does open

    I have a piece of code in JavaFX which creates a small web browser. The web page is shown in a web view. I have managed to get custom context menu popped up by right clicking on the links on the webpage. The context menu has a menu item labeled "open in new tab". However, I have a strange problem. If I click on the "open in new tab", sometimes related webpage opens in new tab, but other times it fails to do so. Please note that if I implement the functionality without any context menu and using right click alone directly on the link, the link opens every time in new tab. How do we describe the situation? Is it a threading issue? I have tested the code on Windows 7 64-bit and Windows 8.1 64-bit. I am using BlueJ on Java 17 and JavaFX 20.
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.scene.web.WebEngine;
    import javafx.scene.web.WebView;
    import javafx.scene.web.WebEvent;
    import javafx.stage.Stage;
    import javafx.scene.input.MouseButton;
    import javafx.scene.input.MouseEvent;
    import javafx.event.EventHandler;
    import javafx.event.ActionEvent;
     
    public class QuickTab extends Application
    {
        TabPane tpane;
        TextField urlField;
        WebView webView;
        String url;
        public void start(Stage stage)
        {
            tpane = new TabPane();
            Tab urlTab = new Tab();
            HBox box = new HBox();
            urlField = new TextField();
            urlField.setPromptText("Enter URL");
            Button goBtn = new Button("Go");
            goBtn.setOnAction(e -> load_page());
            box.getChildren().addAll(urlField, goBtn);
            urlTab.setGraphic(box);
            tpane.getTabs().add(urlTab);
     
            stage.setScene(new Scene(tpane, 400, 300));
            stage.show();
        }
        private void load_page()
        {
            webView = new WebView();
            WebEngine engine = webView.getEngine();
            webView.setContextMenuEnabled(false);//***
            Tab tab = new Tab("home tab");
            tab.setContent(webView);
            tpane.getTabs().add(tab);      
     
            engine.setOnStatusChanged((WebEvent<String> event) -> {
                url = event.getData();
     
            });
            engine.load(urlField.getText());       
            webView.setOnMouseClicked(new TabOpener());
        }
        private class TabOpener implements EventHandler<MouseEvent>
        {
     
            public void handle(MouseEvent e)
            {
                if(e.getButton() == MouseButton.SECONDARY)
                {
                    ContextMenu cmenu = new ContextMenu();
                    MenuItem item = new MenuItem("open in new tab"); 
                    cmenu.getItems().addAll(item);
                    cmenu.show(webView, e.getScreenX(), e.getScreenY());                    
                    item.setOnAction(event -> {
                        Platform.runLater(() -> load_newPage());
                    });
                }
                e.consume();
            }
            private void load_newPage()
            {
                WebView newView = new WebView();
                WebEngine newEngine = newView.getEngine();
                Tab newTab = new Tab(url);
                tpane.getTabs().add(newTab);
                newTab.setContent(newView);
                newEngine.load(url);
     
            }
        }    
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Clicking on menu item "open in new tab" on webview does not open webpage sometimes, other time it does open

    Have you tried debugging the code to see what is happening?
    For example what are the values in the url variable? Add some print statements to show its value when it is changed and when it is used.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2023
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Clicking on menu item "open in new tab" on webview does not open webpage sometimes, other time it does open

    Yes. I found that the "url" is not reaching to the load_newPage() method.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Clicking on menu item "open in new tab" on webview does not open webpage sometimes, other time it does open

    Yes, the problem seems to be that url is changed many times by the setOnStatusChanged method. I don't know how to change that.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2023
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Clicking on menu item "open in new tab" on webview does not open webpage sometimes, other time it does open

    Thanks for your response. But can you suggest an alternative way to achieve the same goal? The goal is that I should be able to open a context menu by right clicking the link on the webView, the context menu would have menu item "open link in new tab" and clicking on this menu item should open the link in new tab. If using setOnStatusChange is not the solution then I am ready to use alternative method.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Clicking on menu item "open in new tab" on webview does not open webpage sometimes, other time it does open

    Sorry, I am not familiar with JavaFX classes. My only suggestion is to read the API doc to see what other methods may be useful.

    The only suggestion I have is to not have the setOnStatusChange method change url to be an empty String. Not a very good solution but it would be something.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Cannot call method "open"
    By nickarn in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2014, 02:40 PM
  2. "Ask a Question" menu item dumps into "What's Wrong With My Code?"
    By GregBrannon in forum Forum Updates & Feedback
    Replies: 1
    Last Post: August 6th, 2013, 03:37 AM
  3. Im trying to open a new GUI window by clicking on a button
    By amzwans in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 10th, 2011, 11:33 PM
  4. I'm trying to open a "FileOpenDialog"
    By Blackbird in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 7th, 2011, 05:22 PM
  5. Suddenly getting "Too Many Open File" error
    By newbie14 in forum Java Networking
    Replies: 4
    Last Post: December 12th, 2010, 11:12 PM

Tags for this Thread