There is nothing easier to start on the javafx tutorial than to start with Hello World application. First and foremost you have to download netbeans with javaFX. Just follow the instructions in installation. After you have the IDE, you can start creating your first JAVAFX application.
JAVAFX Getting Started
- Open Netbeans
- Go to File –> New Project
- On the Categories List, Select JavaFX and then on the Projects select the JavaFX Application.
- Click Next
- Now you have to name your Project. Put simply HelloWorld as Project Name and Click Finish.
- A new java code will automatically be generated and you would find it in HelloWorld –> Source Packages –>heeloworld –> HelloWorld. The code would be similar to this.
package helloworld; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; /** * * @author Ryan Salvador * This is a beginner's sample application * using JAVAFX * */ public class HelloWorld extends Application { @Override public void start(Stage primaryStage) { Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); StackPane root = new StackPane(); root.getChildren().add(btn); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } /** * The main() method is ignored in correctly deployed JavaFX application. * main() serves only as fallback in case the application can not be * launched through deployment artifacts, e.g., in IDEs with limited FX * support. NetBeans ignores main(). * * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }
- Run the program and you would get the following console output:
ant -f C:\\Users\\ryan\\Documents\\NetBeansProjects\\HelloWorld jfxsa-run init: deps-jar: Created dir: C:\Users\ryan\Documents\NetBeansProjects\HelloWorld\build Updating property file: C:\Users\ryan\Documents\NetBeansProjects\HelloWorld\build\built-jar.properties Created dir: C:\Users\ryan\Documents\NetBeansProjects\HelloWorld\build\classes Created dir: C:\Users\ryan\Documents\NetBeansProjects\HelloWorld\build\empty Created dir: C:\Users\ryan\Documents\NetBeansProjects\HelloWorld\build\generated-sources\ap-source-output Compiling 1 source file to C:\Users\ryan\Documents\NetBeansProjects\HelloWorld\build\classes compile: Created dir: C:\Users\ryan\Documents\NetBeansProjects\HelloWorld\dist Detected JavaFX Ant API version 1.2 Launching <fx:jar> task from C:\Program Files\Java\jdk1.7.0_17\lib\ant-javafx.jar Launching <fx:deploy> task from C:\Program Files\Java\jdk1.7.0_17\lib\ant-javafx.jar jfx-deployment-script: jfx-deployment: jar: Copying 12 files to C:\Users\ryan\Documents\NetBeansProjects\HelloWorld\dist\run125364085 jfx-project-run: Executing com.javafx.main.Main from C:\Users\ryan\Documents\NetBeansProjects\HelloWorld\dist\run125364085\HelloWorld.jar using platform C:\Program Files\Java\jdk1.7.0_17/bin/java Hello World! Hello World!
- As you would have noticed, a new application will run that will look similar like this.
- Pressing the button Say ‘Hello World’ will give a console output of Hello World! as presented in item 7.
Congratulations you now have your first JavaFX Application.