Create JavaFX Project With Netbeans and Scene Builder Tools

 Hello guys, this time I will share step by step how to make JavaFX applications using Netbeans and SceneBuilder tools



Open Netbeans IDE, and create a new project,

select Java with Ant --> Java Application


enter the project name, for example "myFX" 



On the Properties menu select Libraries

Libraries --> Compile --> Classpath+ --> Add Library --> Select JavaFX library



Libraries --> Run --> Modulepath+ --> Add Library --> Select JavaFX library


On the Run Menu fill the VM Option

--module-path "C:\Program Files\Java\JavaFX\javafx-sdk-19\lib" --add-modules javafx.controls,javafx.fxml

Replace the red text with the address of the javaFX library directory on your computer



Next, crate New Empty FXML in your package


check "use Java Controller"


Go to MainClass "MyFX.java" and copy  the following code

 package myfx;  
 import javafx.application.Application;  
 import static javafx.application.Application.launch;  
 import javafx.fxml.FXMLLoader;  
 import javafx.scene.Parent;  
 import javafx.scene.Scene;  
 import javafx.stage.Stage;  
 public class MyFX extends Application {  
   @Override  
   public void start(Stage primaryStage) throws Exception{  
     Parent root = FXMLLoader.load(getClass().getResource("MyFXML.fxml"));  
     primaryStage.setTitle("my JavaFX App");  
     primaryStage.setScene(new Scene(root));  
     primaryStage.show();  
   }  
   public static void main(String[] args) {  
     // TODO code application logic here  
      launch(args);  
   }  
 }  


open myFXML.fxml on the SceneBuilder

Add button , Properties --> Text, change text to "Hello JavaFX"



fill Code --> fx:id "btn"




Save your file in SceneBuilder

Back to Netbeans,

In myFXMLController, add the following code

 @FXML  
   private void initialize(ActionEvent event) {  
     System.out.println("Hello JavaFX");  
   }  

Save your project, and Run project


Download my source code HERE

إرسال تعليق

0 تعليقات