WebBrowser / Java Browser

9

How do I put a sort of 'WebBrowser [.NET]' into a Java project?

I know there are free rendering engines like Gecko, which is used in Firefox. But I do not even know where to start, even if I get a free engine ready.

I wanted a light from any of you who could do that. I looked for several tutorials out there on how to do this, but none solved my problem!

    
asked by anonymous 10.06.2015 / 21:05

2 answers

9

Nickolas, I do not know if this is what you need, but I think so.

JavaFX provides a component called WebView, which with the help of WebEngine can be used to load web pages along with building the DOM and executing the necessary JavaScript. For Swing applications it provides another component called JFXPanel which can be used to embed JavaFX components in Swing applications and in turn JFXPanel is added to JFrame or other Swing containers.

This example below can be downloaded from the link page. need.

importjavafx.application.Platform;importjavafx.embed.swing.JFXPanel;importjavafx.scene.Scene;importjavafx.scene.layout.BorderPane;importjavafx.scene.web.WebView;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;publicclassSwingHtmlDemo{publicstaticvoidmain(String[]args){SwingUtilities.invokeLater(newRunnable(){@Overridepublicvoidrun(){ApplicationFramemainFrame=newApplicationFrame();mainFrame.setVisible(true);}});}}/***MainwindowusedtodisplaysomeHTMLcontent.*/classApplicationFrameextendsJFrame{JFXPaneljavafxPanel;WebViewwebComponent;JPanelmainPanel;JTextFieldurlField;JButtongoButton;publicApplicationFrame(){javafxPanel=newJFXPanel();initSwingComponents();loadJavaFXScene();}/***InstantiatetheSwingcompoentstobeused*/privatevoidinitSwingComponents(){mainPanel=newJPanel();mainPanel.setLayout(newBorderLayout());mainPanel.add(javafxPanel,BorderLayout.CENTER);JPanelurlPanel=newJPanel(newFlowLayout());urlField=newJTextField();urlField.setColumns(50);urlPanel.add(urlField);goButton=newJButton("Go");

    /**
     * Handling the loading of new URL, when the user
     * enters the URL and clicks on Go button.
     */
    goButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        Platform.runLater(new Runnable() {
          @Override
          public void run() {
            String url = urlField.getText();
            if ( url != null && url.length() > 0){
                webComponent.getEngine().load(url);
            }
          }
        });

      }
    });

    urlPanel.add(goButton);
    mainPanel.add(urlPanel, BorderLayout.NORTH);

    this.add(mainPanel);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(700,600);
  }

  /**
  * Instantiate the JavaFX Components in
  * the JavaFX Application Thread.
  */
  private void loadJavaFXScene(){
    Platform.runLater(new Runnable() {
      @Override
      public void run() {

        BorderPane borderPane = new BorderPane();
        webComponent = new WebView();

        webComponent.getEngine().load("http://google.com/");

        borderPane.setCenter(webComponent);
        Scene scene = new Scene(borderPane,450,450);
        javafxPanel.setScene(scene);

      }
    });
  }
}
    
15.06.2015 / 19:11
0

This does not answer the question , it is an additional answer for anyone looking for something that uses javascript but how many people will find this on Google is looking for a way to automate per line command a complete WebKit browser, it is interesting to consider PhantomJS .

PhamtomJS has a version for Windows, Linux and Windows, and in situations where more than simple web scrapping is required, it is very good. It literally emulates a complete browser, takes print screen and other things.

The code below, for example, accesses SOPT and takes a screenshot and saves it in the local folder

// $ phantomjs nomedoarquivo.js
var page = require('webpage').create();
page.open('http://pt.stackoverflow.com/', function() {
  page.render('sopt.png');
  phantom.exit();
});
    
20.06.2015 / 22:54