JavaFXDev: NetBeans Platform with JavaFX 2.0ea

Post to Twitter

Hi!

Alan O’Leary shows in his blog a WebView integration in Swing. It is a not good documented feature, how to integrate JavaFX 2.0 controls into a swing application. But an integration is a main goal for the JavaFX 2.0 release.

As an enthusiastic NetBeans Platform/RCP developer and JavaFX partner, I work since two days to marriage JavaFX 2.0 and a NetBeans Platform Application. And yes, it works :-)

I’ve created a maven based platform application with a special starter Main.class. I need to launch the JavaFX toolkit system before any other module bootstrapping. The solution here is based on the early access release through the JavaFX partner program. This “best practice” may change significantly between now and the final version. However, I’ll show only a concept, not compilable code.

The solution behind the bootstrapping a NetBeans Platform is based on a FAQ by Tom Wheeler. My Main.class is a JavaFX Application class – I need this Application instance to get rid of from invoke exceptions. The created Stage object by the Launcher can be ignored.


public class Main extends Application{

  private static final String NB_MAIN_CLASS = "org.netbeans.core.startup.Main";

  public static void main(String[] args) throws Exception {
    // do whatever you need here (e.g. show a custom login form)
    System.out.println("Launch Java FX");
    long ms = System.currentTimeMillis();

    Launcher.launch(Main.class, args); // This is the main start up for JavaFX 2.0

    System.out.println("Launched Java FX in " + (System.currentTimeMillis() - ms) + "ms");

    // once you're done with that, hand control back to NetBeans
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    Class mainClass = Class.forName(NB_MAIN_CLASS, true, classloader);

    Object mainObject = mainClass.newInstance();
    Method mainMethod = mainClass.getDeclaredMethod("main", new Class[]{String[].class});
    mainMethod.invoke(mainObject, (Object) args);
  }

  @Override
  public void start(Stage stage) {
    // Nothing to do, forget the stage....
  }
}

The Main class is in a standard Java archive. This JAR file and all the JavaFX files must be in the platform/core folder.

At runtime all the core-Libs are available to the whole NetBeans Platform modules (and plugins). For the compiler I need a special dependency to the runtime:


<dependency>
 <groupId>sun.javafx</groupId>
 <artifactId>tools</artifactId>
 <version>2.0.0</version>
 <scope>system</scope>
 <systemPath>${basedir}/../../corelauncher/mainlauncher/src/main/lib/jfxrt.jar</systemPath>
</dependency>

The system path depends on your project structure.

Now I can access all the JavaFX classes in a NetBeans module.

Please note, any scene construction needs to be build up in the JavaFX event queue thread. This is not the EventDispatcher-Thread from Swing!

My favorite call to jump in the JavaFX event thread is: Toolkit.getDefault().defer (Runnable) javafx.application.Platform.runLater (Runnable). But the Toolkit class is in a com.* package. IMHO in the future we get an official way to do this.

The creation of a WebView component is pretty similar to the sample from Alan. But I don’t need a stage object:


    Platform.runLater(new Runnable() {

      @Override
      public void run() {
        group = new Group();
        Scene scene = new Scene(group);

        browser = new WebView(new WebEngine());
        browser.getEngine().addChangeListener(PropertyReference.WILDCARD, new ChangeListener() {

          @Override
          public void handle(Bean paramBean, PropertyReference paramPropertyReference) {
            if ("title".equals(paramPropertyReference.getName())) {
              EventQueue.invokeLater(new Runnable() {
                // Jump to Swing EventDispatcher...
                @Override
                public void run() {
                  BrowserTopComponent.this.setDisplayName(browser.getEngine().getTitle());
                }
              });
            }
            if ("url".equals(paramPropertyReference.getName())) {
              EventQueue.invokeLater(new Runnable() {
                // Jump to Swing EventDispatcher...
                @Override
                public void run() {
                  String url = browser.getEngine().getUrl();
                  tfUrl.setText(url);
                  addHistory(url);
                }
              });
            }
          }
        });

        group.getChildren().add(browser);
        group.setScaleX(0.8d);
        group.setScaleY(0.8d);
        Reflection r = new Reflection();
        r.setTopOffset(8);
        group.setEffect(r);

        scene.setFill(javafx.scene.paint.Color.BLACK);
        browser.setWidth(panel.getWidth());
        browser.setHeight(panel.getHeight());

        panel.setScene(scene);
      }
    });

    panel.addComponentListener(new ComponentAdapter() {
      @Override
      public void componentResized(ComponentEvent e) {
        Platform.runLater (new Runnable() {
          // Jump from Swing-EventDispatcher to the JavaFX Thread:
          @Override
          public void run() {
            browser.setWidth(panel.getWidth());
            browser.setHeight(panel.getHeight());
          }
        });
      }
    });

I’ve added some useful listeners. Please aware the switches between different threads (Swing and JavaFX).

The result is a beautiful NetBeans Platform application with an embedded JavaFX 2.0 WebView:

PS.: I like the Twitter message from Dean Riverson: “Ok, I call a moratorium on rotating and reflecting WebView…” (origin).  –

I have to write 100 times:

I’ll never rotate and reflect WebView again, I’ll never rotate and reflect WebView again,I’ll never rotate and reflect WebView again,I’ll never rotate and reflect WebView again,I’ll never rotate and reflect WebView again,I’ll never rotate and reflect WebView again,I’ll never rotate and reflect WebView again,I’ll never rotate and reflect WebView again,I’ll never rotate and reflect WebView again,I’ll never rotate and reflect WebView again, …
Dieser Beitrag wurde unter JavaFX, Maven, NetBeans Plattform abgelegt und mit , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

15 Antworten auf JavaFXDev: NetBeans Platform with JavaFX 2.0ea

  1. Hi Aljoscha,

    Just two quick points:
    1) I didn’t post the JavaFX WebView post, it was Alan O’Leary.
    2) Rather than use Toolkit, consider using javafx.application.Platform – which is the public API you mentioned should be in there.

    Keep up the great work! :-)

  2. Ryan sagt:

    Very cool. I need to know if WebView can be run full screen, and if it can be used on OSX, Linux and Solaris. I’d also like to see an example of JavaScript using objects and methods from the host Java application, and the host application calling JavaScript callback functions.

    • Hi Ryan!

      It is possible to set a Stage or the NetBeans Platform MainWindow to a fullsceen mode. In a simple Java Swing application you can create a undecorated window in full screen mode. Paste a WebView, that’s it.

      I’ll create a short example to show JS functionality.

  3. Great stuff … keep them coming.

    Since , as one of the mere mortals I couldn’t get into the JavaFX 2.0 EA … could you please provide some feedback about the “feel” of the WebView integration (speed, interaction, heavyweight vs. lightweight …etc) ? Is the WebView stuff based on JWebPanel ?

    More advanced topics like: JavaScript integration/callbacks, DOM events … will be more then welcomed too :)

    • Hi!

      Speed is good. I can’t see any heavy- & lightweight issues. Menus, Tooltips and slided windows (a special kind of window state in the NetBeans Platform) are overlapping as expected. Whether JWebPanel is the former WebView, I can only guess. IMHO yes. Script calling is possible (with returning an object). The DOM (via org.w3c.dom) is readable and writable. EventTarget is implemented. But I’ll test and publish it :-)

      br, josh.

  4. Pingback: Java desktop links of the week, March 7 | Jonathan Giles

  5. Pingback: JavaFX links of the week, March 7 // JavaFX News, Demos and Insight // FX Experience

  6. Anonymous sagt:

    Does the WebView support Flash and Java applets?

  7. Jens Weintraut sagt:

    Hi!

    Currently we are evaluating several possibilities to enable web rendering in our swing application. So this blog post was really interesting for us. JavaFX’ WebView seems to be the only future-ready way to do this. And it’s free. So I was really keen to try it myself and waited for the beta release, which now is available.
    But I’m stuck. The call to “JFXPanel.setScene(Scene)” closes my application immediately without any exception. Perhaps someone of you is faced with the same problem?

    Thanks in advance
    Jens

  8. Joel Moberg sagt:

    I have been looking for an embed browser plugin to netbeans. Could this be it? Can you send me a pluginfile?

  9. Cedric sipa sagt:

    Hi
    i’m not sure this is a place to make my request but i hope someone will help me
    i’m tring for severals day now to convert this javafx1.3 code to javafx 2.0
    http://blogs.oracle.com/rakeshmenonp/entry/javafx_image_viewer
    can someone please help me to convert it?
    thanks

  10. Pingback: JavaFX 2 in NetBeans Platform Application with JFXPanel | 0xcafebabe's space

  11. Pingback: JavaFX 2.0 - Bizzteams - Страница 4

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

eMail-Benachrichtigung bei weiteren Kommentaren.
Auch möglich: Abo ohne Kommentar.