Webbrowser/Java Browser

Asked

Viewed 3,391 times

9

How I put a kind of a 'Webbrowser[. NET]' in a Java project?

I know that there are free rendering engines like Gecko, which is used in Firefox. But I don’t even know where to start, even getting a free engine and ready.

I wanted a light from some of you who know how to do this. I looked around for several tutorials on how to do this, but none solved my problem!

  • http://stackoverflow.com/questions/48249/is-there-a-way-to-embed-a-browser-in-java

  • Swing, SWT, Javafx?

  • Wish, if possible, in java swing!

2 answers

9


Nickolas, I don’t know if this is what you need, but it seems to me.

Javafx provides a component called Webview , which with the help of Webengine can be used to load web pages along with the DOM construction and run the required Javascript. For Swing applications it provides another component called Jfxpanel that can be used to incorporate Javafx components into Swing applications and in turn Jfxpanel is added to Jframe or other Swing containers.

This example down there taken from the site http://www.javabeat.net/embedding-html-into-java-swing-applications/ can be exactly what you need.

WebView

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SwingHtmlDemo {
  public static void main(String [] args){

    SwingUtilities.invokeLater(new Runnable() {
    @Override
      public void run() {
        ApplicationFrame mainFrame = new ApplicationFrame();
        mainFrame.setVisible(true);
      }
    });

  }

}

/**
* Main window used to display some HTML content.
*/
class ApplicationFrame extends JFrame{

  JFXPanel javafxPanel;
  WebView webComponent;
  JPanel mainPanel;

  JTextField urlField;
  JButton goButton;

  public ApplicationFrame(){

    javafxPanel = new JFXPanel();

    initSwingComponents();

    loadJavaFXScene();
  }

  /**
  * Instantiate the Swing compoents to be used
  */
  private void initSwingComponents(){
    mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(javafxPanel, BorderLayout.CENTER);

    JPanel urlPanel = new JPanel(new FlowLayout());
    urlField = new JTextField();
    urlField.setColumns(50);
    urlPanel.add(urlField);
    goButton = new JButton("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);

      }
    });
  }
}
  • And in swing, there’s some?

  • 1

    @Nickolascarlos In swing "pure" I do not know how to tell you and I even researched to know, but for what via solution is this same Jfxpanel, and Coo could see in the code it is 100% compatible with swing and easy implementation...try to use, who knows meets your needs in a good.

0

That doesn’t answer the question, is an additional answer for anyone looking for something to use javascript but how many people will find this on Google is looking for a way to automate by command line a full browser, like Webkit, it is interesting to consider the Phantomjs.

The PhamtomJS has version for Windows, Linux and Windows, and in situations where it is necessary more than simple web Scrapping, it is very good. Allows you to literally emulate a full browser, take print screen and other things.

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

// $ phantomjs nomedoarquivo.js
var page = require('webpage').create();
page.open('/', function() {
  page.render('sopt.png');
  phantom.exit();
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.