Over a million developers have joined DZone.

How to Integrate JavaFX into the NetBeans Platform Toolbar

DZone's Guide to

How to Integrate JavaFX into the NetBeans Platform Toolbar

· Java Zone ·
Free Resource

Secure your Java app or API service quickly and easily with Okta's user authentication and authorization libraries. Developer accounts are free forever. Try Okta Instead.

Next up, now that we know how to replace the NetBeans Platform menubar with a new one from JavaFX (see yesterday's article on this theme), let's see how to achieve the same for the toolbar:

The toolbar above not only uses JavaFX, but is also tabbed. So, specifically, the JavaFX classes TabPane, Pane, ToolBar, and Button are used in combination with Actions loaded from the NetBeans Platform filesystem's "Toolbars" folder, within a JFXPanel, which is placed in the RootPane of the NetBeans Platform, using a layout made available by the instructions provided by Chris Böhme from PinkMatter, as described in detail, with code, here:


Therefore, the starting point for creating the result shown in the screenshot above is to get all the code described in the link above into a NetBeans module. That means you'll have an Installer class and a MyRootPaneLayout class.

However, the ToolbarComponentProvider class is pretty different:

public abstract class ToolbarComponentProvider {

    public abstract JComponent createToolbar();

    public static ToolbarComponentProvider getDefault() {
        ToolbarComponentProvider provider =
        if (provider == null) {
            provider = new DefaultToolbarComponentProvider();
        return provider;

    private static class DefaultToolbarComponentProvider
            extends ToolbarComponentProvider {
        public JComponent createToolbar() {
            final JFXPanel toolbarFXPanel = new JFXPanel();
            Platform.runLater(new Runnable() {
                public void run() {
                    Scene scene = new Scene(new VBox(), 400, 70);
                    TabPane pane = new TabPane();
                    FileObject menuFolder = FileUtil.getConfigFile("Toolbars");
                    FileObject[] mainToolBarKids = menuFolder.getChildren();
                    for (FileObject oneMainToolBarKid
                            : FileUtil.getOrder(Arrays.asList(mainToolBarKids), true)) {
                        Tab tab = new Tab(oneMainToolBarKid.getName());
                        ToolBar newToolbarFromFileObject = new ToolBar();
                        try {
                        } catch (FileNotFoundException ex) {
                    ((VBox) scene.getRoot()).getChildren().add(pane);
            return toolbarFXPanel;

    private static void buildToolBarStructure(
            FileObject oneMainToolBarKid,
            ToolBar newToolbarFromFileObject) throws FileNotFoundException {
        for (FileObject oneSecondLevelToolBarKid : oneMainToolBarKid.getChildren()) {
            final Action instanceObj =
            //Name stuff:
            String name = (String) instanceObj.getValue(Action.NAME);
            String cutAmpersand = Actions.cutAmpersand(name);
            //Icon stuff:
            Icon icon = (Icon) instanceObj.getValue(Action.SMALL_ICON);
            ImageView iv = new ImageView();
            //Optionally, include the name in the button:
            //Button toolbarButton = new Button(cutAmpersand);
            Button toolbarButton = new Button();
            if (icon != null) {
                java.awt.Image awtImage = ImageUtilities.icon2Image(icon);
                Image image = new Image(getInputStream(awtImage));
            toolbarButton.setOnAction(new EventHandler<ActionEvent>() {
                public void handle(final ActionEvent t) {
                    try {
                        SwingUtilities.invokeAndWait(new Runnable() {
                            public void run() {
                                java.awt.event.ActionEvent event =
                                        new java.awt.event.ActionEvent(
                    } catch (InterruptedException ex) {
                    } catch (InvocationTargetException ex) {

    private static InputStream getInputStream(java.awt.Image image) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        InputStream fis = null;
        try {
            ImageIO.write((RenderedImage) image, "png", os);
            fis = new ByteArrayInputStream(os.toByteArray());
        } catch (IOException ex) {
        return fis;


That's all. Now you'll have a tabbed toolbar that embeds the JFXPanel and then embeds a TabPane, Pane, and ToolBar, on top of each other, plus a Button for each Action registered in the "Toolbars" folder of the NetBeans Platform filesystem, to attain the result shown in the screenshot at the start of this article.

Secure your Java app or API service quickly and easily with Okta's user authentication and authorization libraries. Developer accounts are free forever. Try Okta Instead.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}