IntelliJ IDEA 2020.1 Help

Develop a basic JavaFX application

In this tutorial, we transform the sample application created by IntelliJ IDEA into a basic JavaFX Hello World application.

For more information on how to configure a sample application, refer to Create a new JavaFX project.

Rename the Controller class

To adjust the sample application to your needs, you may want to start with renaming the files. For example, let's rename the Controller class to SampleController.

  1. In the editor, place the caret at the class name and from the main menu, select Refactor | Rename Shift+F6.

    Controller is now highlighted.

  2. Type Sample before Controller and Press Enter to indicate that you have completed the refactoring.

  3. Switch to sample.fxml in the editor and note that the value of the GridPanel fx:controller attribute has changed from "sample.Controller" to "sample.SampleController".

Add a button

In the interface of our application, let's define a button that is going to show Hello World! after you click it.

Add the following two elements between the opening and closing <GridPane> tags in the file sample.fxml:

<Button text="Say 'Hello World'" onAction="#sayHelloWorld"/> <Label GridPane.rowIndex="1" fx:id="helloWorld"/>

sayHelloWorld is shown in red and helloWorld is highlighted. This means that IntelliJ IDEA cannot resolve the corresponding references. Let's fix this.

Missing references in the added code in the editor

Complete the code for SampleController

Let's define the field helloWorld in the SampleController class. We will also add the corresponding event handler method sayHelloWorld that will set the text for the helloWorld label.

  1. In sample.fxml, place the caret at helloWorld and press Alt+Enter.

  2. Select Create Field 'helloWorld'.

    IntelliJ IDEA switches to SampleController.java where the IDE has added the declaration of the field helloWorld. Press Enter to quit the refactoring mode.

    Also note the import statement that has just been added import javafx.scene.control.Label; and the icon the icon in the gutter to the left of the field declaration. This is a navigation icon; click it to go back to sample.fxml.

  3. Place the caret at sayHelloWorld and press Alt+Enter.

  4. Select Create Method 'void sayHelloWorld(ActionEvent)'.

    IntelliJ IDEA adds the corresponding method declaration to SampleController.java.

  5. Press Shift+Enter to quit the refactoring mode and start a new line.

  6. Type the following code to set the text for the label:

    helloWorld.setText("Hello World!");

Run the application

At this step, the code is ready. Let's run the application to see the result.

  1. Click the Run button on the toolbar or press Shift+F10.

    The application window now shows the Say 'Hello World' button.

  2. Click this button to display the Hello World! text.

Style the UI with CSS

Let's change the appearance of the UI by adding a style sheet and defining formatting styles.

  1. In the file sample.fxml, add a reference to sample.css that we haven't created yet.

    Add the stylesheets attribute within the opening <GridPane> tag:

    stylesheets="/sample/sample.css"
  2. Press Alt+Enter and select Create File sample.css

  3. When the .css file is created, add the following style definitions.

    The first of the styles makes the background in the application window "bisque" and the second one - sets the font size for the Hello World! text to 20 pixels.

    .root { -fx-background-color: bisque; } .label { -fx-font-size: 20; }
  4. Click the Run button on the toolbar or press Shift+F10 to run the application.

Last modified: 8 April 2020