Hello Friends, Today we are going to see how to create and run JavaFX HelloWorld with NetBeans and Gradle. Actually I had developed ZatackCrawler a data scrapper tool few years ago when I was learning JavaFX. I had also shared this tool in 2017 that you can check here. But few days ago I had to use this tool but surprisingly I was unable to run this tool. Then I have done some research and found that JavaFX is removed from Java 11 but it is still available till Java 10, so this tool should work till Java 10. Because I was using Java 11 that’s why I was not able to run this tool. Then I thought to migrate this tool to Java 11 but I faced few problems while migration to JavaFX so I thought to share this HelloWorld Example. JavaFX is now maintained by gluon. I have used Gradle build tool in this example because it’s easy to put all the depedencies in single gradle file and it downloads the dependencies automatically. You can also use Maven or just add javafx jars in your libraries.
Now, Let’s Code
build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
plugins { id 'application' id 'org.openjfx.javafxplugin' version '0.0.8' } javafx { version = "11.0.2" modules = [ 'javafx.controls', 'javafx.fxml' ] } mainClassName = 'com.zatackcoder.Main' repositories { jcenter() } dependencies { testImplementation 'junit:junit:4.13' } |
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.zatackcoder; /** * * @author rajesh */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { MainApp.main(args); } } |
MainApp.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.zatackcoder; import javafx.application.Application; import javafx.application.Platform; import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.stage.WindowEvent; /** * * @author rajesh */ public class MainApp extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("scene.fxml")); Scene scene = new Scene(root); stage.setOnCloseRequest(new EventHandler<WindowEvent>() { @Override public void handle(WindowEvent t) { Platform.exit(); System.exit(0); } }); stage.setScene(scene); stage.setTitle("Hello JavaFX"); stage.show(); } public static void main(String[] args) { launch(args); } } |
scene.fxml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Label?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zatackcoder.SceneController"> <children> <Label fx:id="label" layoutX="282.0" layoutY="192.0" text="Label" /> </children> </AnchorPane> |
SceneController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.zatackcoder; import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Label; /** * FXML Controller class * * @author rajesh */ public class SceneController implements Initializable { @FXML private Label label; /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb) { label.setText("Hello JavaFX 11\nRunning on Java 11"); } } |
File Structure
Output
Output using IDE | Output from Distributions folder using Terminal |
NetBeans Project Download
JavaFX HelloWorld NetBeans Project
Thanks friends
Your queries & suggestions are welcome in comments section
Please don’t forget to share if you find this helpful
Comments