NetBeans RCP und JavaFX Tutorial - English

Anleitungen, Beispiele und Hilfestellungen zu NetBeans

Moderator: wegus

Antworten
Benutzeravatar
smurfi
Site Admin
Beiträge: 1623
Registriert: 29.06.2006, 11:33
Wohnort: Wuppertal
Kontaktdaten:

NetBeans RCP und JavaFX Tutorial - English

Beitrag von smurfi » 13.08.2013, 11:31

I have decided my tutorial NetBeans RCP and JavaFX also be set to English for disposal. The German version can be find here http://www.netbeans-forum.de/viewtopic.php?f=13&t=6408

This here is a beginners tutorial on JavaFX NetBeans RCP and explains it step by step as NetBeans and JavaFX can be used together.

First we create a new Netbeans Project 'File' -> 'New Project'
New Project.png
New Project.png (46.01 KiB) 5452 mal betrachtet
From the categories choose ''NetBeans Platform Application', you find it under 'Categories' -> 'NetBeans Modules'
New Project-NetBeans Platform Application.png
New Project-NetBeans Platform Application.png (44.72 KiB) 5452 mal betrachtet
Now assign a 'Project Name', I use 'JavaFX-Application', but you can also use anything else.
For this example it is easier you use the same name.
New Project-Name and Location.png
New Project-Name and Location.png (38.08 KiB) 5452 mal betrachtet
When you click 'Finish' it also looking like this
NetBeans-1.png
NetBeans-1.png (22.05 KiB) 5452 mal betrachtet
Next, we need to add a new module, this is done by right-clicking at 'Modules' -> 'Add New'.
I used here as a 'Project name' - 'JavaFX', the rest we leave unchanged.
By clicking on 'Next' it goes to the 'Basic Module Configuration'
New Module Project-Basic Modul Configuration.png
New Module Project-Basic Modul Configuration.png (35.03 KiB) 5452 mal betrachtet
Click on 'Finish' and the basic skeleton of a NetBeans RCP application is done, you can already start while [F6], but does not make as much sense at this point.
NetBeans-2.png
NetBeans-2.png (29.88 KiB) 5452 mal betrachtet
Next, we integrate JavaFX in our RCP project that we do as we choose with a right click on 'Modules' 'Add a New Library'.
In the following window, the JavaFX jar file is then selected.
New Library Wrapper Module Project.png
New Library Wrapper Module Project.png (32.38 KiB) 5452 mal betrachtet
In my Windows7 it is under 'C:\Program Files\Java\jdk1.7.0_25\jre\lib\jfxrt.jar' to find, otherwise you can also just search for the 'jfxrt.jar'.
The next screen can be left as is
New Library Wrapper Module Project-2.png
New Library Wrapper Module Project-2.png (39.39 KiB) 5452 mal betrachtet
Now specify the 'Code Name Base' and press 'Finish'.

In the module 'JavaFX' we must add a dependency for the previously added module.
This we click with the right mouse button on 'JavaFX' and go to 'Properties'.
Project Properties - JavaFX.png
Project Properties - JavaFX.png (32.71 KiB) 5452 mal betrachtet
Now 'Libraries' -> 'Add Dependency' and the modules 'jfxrt' select and finish with 'OK'.
Add Module Dependency.png
Add Module Dependency.png (30.6 KiB) 5452 mal betrachtet
Project Properties - JavaFX-2.png
Project Properties - JavaFX-2.png (40.2 KiB) 5452 mal betrachtet
In the NetBeans Project view click with the right mouse button on 'de.mkroll-javafx'
NetBeans-3.png
NetBeans-3.png (21.06 KiB) 5452 mal betrachtet
and go to 'New' -> 'Window'
New Windows.png
New Windows.png (37.2 KiB) 5452 mal betrachtet
Under 'Window Position' we choose 'editor' and let rest as unchanged.
In the window that still a 'Class name prefix' specify 'icon' I just let empty, and conclude with 'Finish'.
New Windows-2.png
New Windows-2.png (35.42 KiB) 5452 mal betrachtet
Next, we create a JavaFX page, I use JavaFX Scene Builder in a preview version.
JavaFX Scene Builder.png
JavaFX Scene Builder.png (192.29 KiB) 5452 mal betrachtet
After starting the JavaFX Scene Builder is actually already directly a page that you can use, here we choose from the Library a label field and drag it onto the sheet.
JavaFX Scene Builder-Label.png
JavaFX Scene Builder-Label.png (16.55 KiB) 5452 mal betrachtet
JavaFX Scene Builder-Label2.png
JavaFX Scene Builder-Label2.png (164.16 KiB) 5452 mal betrachtet
This we now save directly to the directory where our 'EditorWindowTopComponent' is.
What we need to do now is create us a 'controller class', NetBeans does not support this yet so we have to do it themselves in an RCP application.
For this we create a class 'JavaFXPageController' in the same package, it should now look something like this
NetBeans-4.png
NetBeans-4.png (68.61 KiB) 5452 mal betrachtet
Now we change the 'JavaFXPageController' class as follows and save it.

Code: Alles auswählen

package de.mkroll.javafx;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;

/**
 *
 * @author mkroll
 */
public class JavaFXPageController implements Initializable { 

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
    }   
}
Next, we still fit our 'EditorWindowTopComponent', so this opens directly our JavaFX Page.
We make now the following changes to our 'EditorWindowTopComponent', whereby 'init ()' and 'create scene' added.

Code: Alles auswählen

public final class EditorWindowTopComponent extends TopComponent {
    private JavaFXPageController controller;
    private JFXPanel fxPanel;
    
    public EditorWindowTopComponent() {
        initComponents();
        setName(Bundle.CTL_EditorWindowTopComponent());
        setToolTipText(Bundle.HINT_EditorWindowTopComponent());
        
        setLayout(new BorderLayout());
      init();
    }
    /**
     * JavaFX init
     */
    public void init() {
        fxPanel = new JFXPanel();
        add(fxPanel, BorderLayout.CENTER);
        Platform.setImplicitExit(false);
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                createScene();
            }
        });
    }

    /**
     * Create JavaFX scene
     */
    private void createScene() {
        try {
            URL location = getClass().getResource("/de/mkroll/javafx/JavaFXPage.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 = (JavaFXPageController) fxmlLoader.getController();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
Now we have the JavaFXPage still make known our controller class
JavaFX Scene Builder-ControllerClass.png
JavaFX Scene Builder-ControllerClass.png (213.17 KiB) 5452 mal betrachtet
If we have now saved everything we should be able to start our application.
Our editor window will be find in the menu under 'Window' -> 'Editor Window'

That was the first part of my little introduction of JavaFX and NetBeans RCP.
NetBeansRCP.png
NetBeansRCP.png (52 KiB) 5452 mal betrachtet

Antworten