JavaFX - 1 - "Hello World!"


    As Oracle claims "JavaFX is a set of graphics and media packages that enables developers to design, create, test, debug, and deploy rich client applications that operate consistently across diverse platforms." In other words JavaFX can be used to create applications with great user experience. In this series of posts I'm going to demonstrate some sample applications using JavaFX. As the first step we'll see how to create a JavaFX project using IntelliJ IDEA.

    Open IntelliJ IDEA and select "Create New Project". In the next screen select "JavaFX" from the left hand side panel, select "JavaFX Application" from the right hand side panel and click "Next". Use the next screen to provide a name for the project and set the project location. Click "Finish" to create the project.
 
Project Structure
    The structure of your newly created project should look as the above image. As you can see, IntelliJ has already created a sample JavaFX application for you. So before going any further, run the application by clicking Run 'Main' from the Run menu or pressing SHIFT+F10. You should see an empty window with "Hello World" as the title. CONGRATULATIONS! on your first JavaFX application.

    Now let's look into the application code. You can see the sample application consists of three files from which two are java class files named Main and Controller while the other is a fxml file called sample. In JavaFX these fxml files create the view which is presented to the user. The view elements are structured in fxml format within these files. In our sample.fxml we can see a GridPane element which is one of the container types in JavaFX.

   Each view is associated with a specific Controller by specifying it in the property fx:controller. This controller class contains the application logic for the view. The view sample.fxml uses Controller.java class as the controller and since we haven't added any application logic yet, it appears as an empty class.

   Finally the the Main class class of our project (shown below) is used to setup and launch the application.
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Comments

  1. This is my first visit here and I am really impressed by reading the information content you provided. It is very unique and useful. I really appreciate the time and effort you spend to share this with us! I do hope to read more updates from you. It is my pleasure to read this.

    ReplyDelete
  2. This concept is a good way to enhance the knowledge.thanks for sharing. please keep it up Java online training Bangalore

    ReplyDelete

Post a Comment

Popular posts from this blog

Automatically connect to a dial-up dongle in Windows 7

Set the battery level at which Ubuntu gives the "Battery Low" Warning

Cleaning older documents from Elasticsearch