X

Geertjan's Blog

  • December 9, 2011

Thanks JavaFX: Embedded Browser for NetBeans

Geertjan Wielenga
Product Manager

Want the source code for the above? Take the SwingInterop JavaFX sample I referred to a few blog entries ago and copy the browser-related bits from the sample into a TopComponent and then you're done:

public final class BrowserTopComponent extends TopComponent {
    private Pane browser;
    private static JFXPanel browserFxPanel;
    private static final int PANEL_WIDTH_INT = 675;
    private static final int PANEL_HEIGHT_INT = 400;
    public BrowserTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(BrowserTopComponent.class, "CTL_BrowserTopComponent"));
        setToolTipText(NbBundle.getMessage(BrowserTopComponent.class, "HINT_BrowserTopComponent"));
        setLayout(new BorderLayout());
        browserFxPanel = new JFXPanel();
        add(browserFxPanel, BorderLayout.CENTER);
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                createScene();
            }
        });
    }
    private void createScene() {
        browser = createBrowser();
        browserFxPanel.setScene(new Scene(browser));
    }
    private Pane createBrowser() {
        Double widthDouble = new Integer(PANEL_WIDTH_INT).doubleValue();
        Double heightDouble = new Integer(PANEL_HEIGHT_INT).doubleValue();
        WebView view = new WebView();
        view.setMinSize(widthDouble, heightDouble);
        view.setPrefSize(widthDouble, heightDouble);
        final WebEngine eng = view.getEngine();
        final Label warningLabel = new Label("Do you need to specify web proxy information?");
        eng.load("http://www.oracle.com/us/index.html");
        ChangeListener handler = new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if (warningLabel.isVisible()) {
                    warningLabel.setVisible(false);
                }
            }
        };
        eng.getLoadWorker().progressProperty().addListener(handler);
        final TextField locationField = new TextField("http://www.oracle.com/us/index.html");
        locationField.setMaxHeight(Double.MAX_VALUE);
        Button goButton = new Button("Go");
        goButton.setDefaultButton(true);
        EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                eng.load(locationField.getText().startsWith("http://") ? locationField.getText()
                        : "http://" + locationField.getText());
            }
        };
        goButton.setOnAction(goAction);
        locationField.setOnAction(goAction);
        eng.locationProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                locationField.setText(newValue);
            }
        });
        GridPane grid = new GridPane();
        grid.setPadding(new Insets(5));
        grid.setVgap(5);
        grid.setHgap(5);
        GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
        GridPane.setConstraints(goButton, 1, 0);
        GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
        GridPane.setConstraints(warningLabel, 0, 2, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
        grid.getColumnConstraints().addAll(
                new ColumnConstraints(widthDouble - 200, widthDouble - 200, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true),
                new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true));
        grid.getChildren().addAll(locationField, goButton, warningLabel, view);
        return grid;
    }
    ...
    ...
    ...

However, you also need... JavaFX native libraries. I put them into a new folder named 'bin', within build/cluster/modules. Definitely a smoother solution needed here. Right now, the JavaFX SDK for Windows exists, and I believe a build for Mac OSX as well, from where you can get these native libraries. And jfxrt.jar was the only JAR needed from the JavaFX distro.

Join the discussion

Comments ( 4 )
  • Stefan Friday, December 9, 2011

    Very nice!

    How do you get this JavaFX application running on Linux (Ubuntu)? AFAIK there's no version available yet :-(


  • zhouzx_gogo Saturday, December 10, 2011

    I am very grateful to be able to provide such examples. Also very grateful to make a reply to my question and help me complete my desired results. Seen in the RCP and web applications using javafx, I think I will give users a better user experience.

    Through your blog I learn and use the RCP technology, thank you for your contribution, I hope when they encounter problems again for your help.


  • Susanta Tewari Tuesday, December 27, 2011

    The JFXPanel is not getting resized when I double-click the tab. But other swing JComponents are working fine.


  • Geertjan Tuesday, December 27, 2011

    So you're saying that JavaFX is not as great as Swing? Feel free to blog about that.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.