Tuesday Oct 01, 2013

JavaFX MultiViewElement

Here's my first attempt at integrating the visual JavaFX HTMLEditor on top of HTML files on the NetBeans Platform, e.g., NetBeans IDE.

All the code and note that the two sides aren't synchronized (yet):

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.HTMLEditor;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import org.netbeans.core.spi.multiview.CloseOperationState;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.MultiViewElementCallback;
import org.openide.awt.UndoRedo;
import org.openide.filesystems.FileObject;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;

@MultiViewElement.Registration(displayName = "Visual",
        mimeType = {"text/xhtml", "text/html"},
        persistenceType = TopComponent.PERSISTENCE_NEVER,
        preferredID = "Visual",
        position = 1)
public class JavaFXMVE extends JPanel implements MultiViewElement {

    private FileObject obj;
    private JToolBar tb = new JToolBar();
    private static JFXPanel fxContainer;

    public JavaFXMVE(Lookup lkp) {
        this.obj = lkp.lookup(FileObject.class);
        assert obj != null;
        fxContainer = new JFXPanel();
        Platform.runLater(new Runnable() {
            public void run() {
    private void createScene() {
        HTMLEditor htmlEditor = new HTMLEditor();
        try {
        } catch (IOException ex) {
            Logger.getLogger(JavaFXMVE.class.getName()).log(Level.SEVERE, null, ex);
        StackPane root = new StackPane();
        fxContainer.setScene(new Scene(root));
    public JComponent getVisualRepresentation() {
        return fxContainer;

    public JComponent getToolbarRepresentation() {
        return tb;

    public Action[] getActions() {
        return new Action[0];

    public Lookup getLookup() {
        return obj.getLookup();

    public void componentOpened() {

    public void componentClosed() {

    public void componentShowing() {

    public void componentHidden() {

    public void componentActivated() {

    public void componentDeactivated() {

    public UndoRedo getUndoRedo() {
        return UndoRedo.NONE;

    public void setMultiViewCallback(MultiViewElementCallback callback) {

    public CloseOperationState canCloseElement() {
        return CloseOperationState.STATE_OK;


If someone has ideas or code for synchronizing the two sides, don't be shy, leave a comment at the end of this blog entry!


Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« October 2013 »