From the course: Java for All Platforms: Desktop, Web, and Mobile Development

Creating a JavaFX application

- [Instructor] One common tool for creating Java apps that run on the desktop is called JavaFX. Let's create a JavaFX project from IntelliJ by hitting the New Project button and choosing JavaFX. For the location, I'm going to save in the Exercise Files folder, chapter three, create_project, final. If you're following along with the files, just create a new folder here called start and save it in there. I'll call this name JavaFXExample. I'm going to leave everything else at the default and then hit Next. Here I'm only going to add the ControlsFX library but note that there are others if you choose to go further with JavaFX. From there, I'll hit Finish. And we have our application loaded into IntelliJ. There are a few main working classes here. So in here you'll see HelloApplication.java that creates the window for your application. We'll see that in just a minute. We have the controller that handles the view. So this handles the button click in the application. And we have a reference to a label here. And then we have our fxml file, which actually contains our layout information. So you can see that there's something called a VBox, which is a vertical layout box. It has some padding inside of it. There's a label with an id of welcomeText and there's a button that says Hello! And it has something called onAction that tuns onHelloButtonClick using this hash before it. So that's going to call this method onHelloButtonClick when the button is clicked, which modifies the welcomeText. With that, let's look at running the application. You'll notice that the Run button is grayed out. For JavaFX, you run the applications through a Maven command. So open up the Maven menu on the right, expand your project and then expand Plugins and find javafx, javafxrun. So let's double click that. Then the app is going to run on the desktop. And there we go. We have a desktop app running with JavaFX.

Contents