Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JavaFX FXML Meets the NetBeans Platform

DZone's Guide to

JavaFX FXML Meets the NetBeans Platform

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

Here's the FXML that we'd like to see in a NetBeans Platform TopComponent:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane id="topNode" xmlns:fx="http://javafx.com/fxml"
            fx:controller="org.nb.demo.DemoController">    
    <top>
        <HBox fx:id="topHBox" spacing="10">
                <Label fx:id="myLabel1"
                       textFill="#0076a3"            
                       style="-fx-font: Bold 20 papyrus"/>
                <Label fx:id="myLabel2"
                       textFill="#0076a3"
                       wrapText="true"/>
        </HBox>       
        
    </top>
                     
</BorderPane>

Binding will happen on "fx:id" so "myLabel1" and "myLabel2" will be bound to labels named "myLabel1" and "myLabel2". Here's the related controller, in package "org.nb.demo", as specified in the "fx:controller" attribute above:

package org.nb.demo;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class DemoController implements Initializable {

    @FXML
    private Label myLabel1;
    @FXML
    private Label myLabel2;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        myLabel1.setText("hello");
        myLabel2.setText("world");
    }

}

And, finally, here's how to call the above from a TopComponent:

...
...
...
public final class DemoTopComponent extends TopComponent {

    private DemoController controller;
    private JFXPanel fxPanel;

    public DemoTopComponent() {
        initComponents();
        setName(Bundle.CTL_DemoTopComponent());
        setToolTipText(Bundle.HINT_DemoTopComponent());
        setLayout(new BorderLayout());
        init();
    }

    public void init() {
        fxPanel = new JFXPanel();
        add(fxPanel, BorderLayout.CENTER);
        Platform.setImplicitExit(false);
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                createScene();
            }
        });
    }

    private void createScene() {
        try {
            URL location = getClass().getResource("Demo.fxml");
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(location);
            fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
            Parent root = (Parent) fxmlLoader.load(location.openStream());
            Scene scene = new Scene(root);
            fxPanel.setScene(scene);
            controller = (DemoController) fxmlLoader.getController();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
...
...
...

Result:

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}