Bare Bones JavaFX “Hello World!”

JavaFX is a Java toolkit for creating visually pleasing apps. Java is a computer programming language that is used to write apps for most any Operating System (OS) that you can think of. That includes Windows, MacOS, iOS, Android, Red Hat, Solaris, AIX, FreeBSD, and many others that exist in the wild. JavaFX handles the windows, buttons, pictures, and graphics for those Java apps.

Never written a JavaFX or even a Java program before? Then, this is a good first app. All it does is create a window that shows the message, “Hello World!”

Note: This is a good tutorial to start with for the first-time programmer. If you are looking for a good learning path for the new programmer, check out my JavaFX learning path for new developers.

The first thing you need to do, is install a current version of Eclipse. Eclipse is an Integrated Development Environment, more commonly called an IDE.

IDE’s are the standard tool for developing most software these days. You can write an entire application using just Notepad or TextEdit. However, that is painful, and mostly a waste of time. You’ll want a decent IDE, such as Eclipse.

In the Java realm, three IDE’s reign supreme. There is Eclipse, which I prefer. There is Netbeans, that Oracle maintains. There is also, the costly IntelliJ, that many Java developers swear by. Eclipse and Netbeans are free, so you’ll probably want to stick to one of those for now. Like I said, I like Eclipse for my JavaFX development.

If you are a first time developer, you’ll need a copy of the Java Development Kit (JDK). This is different than the Java Runtime Environment (JRE). There are many more goodies in the JDK than the JRE for anyone looking to create Java applications. It’ll bee a little intimidating at first, but go over to Oracle’s site, and grab a current version of the JDK and install it on your computer.

Eclipse is found over at eclipse.org. Just get the standard Java version of the IDE.

Finally, you’ll need to install the JavaFX plugin for Eclipse. It is called e(fx)clipse. Yes. That is really it’s name. I don’t try pronouncing it. Things work out better that way.

You can get e(fx)clipse installed in your Eclipse IDE by following the instructions on the e(fx)clipse home page.

Once you have the JDK, Eclipse, and e(fx)clipse, you are ready to do some JavaFX programming! As stated above, let’s create a JavaFX Hello World app.

Start be creating a new JavaFX project. You can do that by browsing the menu and opening the New Project wizard.

File->New->Project…

This opens the New Project wizard. Since you’ve install e(fx)clispe, you have the JavaFX Project as one of your options. Select it, and click Next.

New JavaFX Project
Select JavaFX Project in the New Project wizard.

Give it the name of hello-world and click Finished.

Name JavaFX Project
Name your JavaFX project hello-world.

Now your package explorer on the left, should look something like this.

JavaFX Hello World Project
Your JavaFX project in the Package Explorer has everything you need to create a simple JavaFX app.

Click on the Main.java file, and let’s make some minor modifications. Currently, Main.java has the following yucky code.

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
  @Override
  public void start(Stage primaryStage) {
    try {
      BorderPane root = new BorderPane();
      Scene scene = new Scene(root,400,400);
      scene.getStylesheets()
      .add(getClass()
          .getResource("application.css")
          .toExternalForm());
      primaryStage.setScene(scene);
      primaryStage.show();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

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

 

They managed to create a default JavaFX app that does less than a Hello World app. That’s impressive and depressing, all at the same time. Let’s fix the code so it at least says something when the app is run.

Paste the following JavaFX code in to replace the old code.

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Main extends Application {
  @Override
  public void start(Stage stage) {
    
    VBox root = new VBox();
    Label hw = new Label("Hello World!");
    root.getChildren().add(hw);

    Scene scene = new Scene(root,400,400);

    stage.setScene(scene);
    stage.show();
  }

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

 

Now let’s run the new Hello World app. Select run Java Application by first clicking on the Main.java file, then browse the menu to select Run as Java Application.

Run->Run As->Java Application

You should get a JavaFX window with the words, Hello World!

Hello World Window
Admittedly, not the most exciting JavaFX app, but everyone starts somewhere.

Welcome to the world of JavaFX programming!

Remember, if you are new to Java programming, or computer programming in general. I’ve written a learning path with plenty of links to articles and tutorials to get you up-to-speed on writing your own JavaFX applications. Anyone who’s determined enough and willing to put in the practice can become a JavaFX developer. It will take time and study, but don’t give up.

One thought on “Bare Bones JavaFX “Hello World!””

  1. Thank you for sharing knowledge about javafx. Next I want to ask is how I can combine javafx with spring framework. Is this possible, because javafx itself has a thread, and spring also has a thread. Is javafx run in spring, or spring that runs in javafx.

Leave a Reply

Your email address will not be published. Required fields are marked *