JavaFx Helloworld

In this example we will create a simple Helloworld application for JavaFx. I have added inline comments to the code.


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

 * The main class for a JavaFX application extends the
 * javafx.application.Application class.
 * @author
public class HelloWorldJavaFx extends Application {
	public static void main(String[] args) {
		 * launch(args) launchs a standalone application. 
		 * The launch method does not return until the application has exited,
		 * either via a call to Platform.exit or all of the application windows
		 * have been closed.

	 * The start() method is the main entry point for any JavaFX application.
	 * The Stage class is the top-level JavaFX container.
	public void start(Stage primaryStage) throws Exception {
		// Set a title
		primaryStage.setTitle("Hello World!");
		// declare root stackPane
		// the root node is a StackPane object(resizable layout node)
		StackPane rootPane = new StackPane();

		// The JavaFX Scene class is the container for all content.
		// the content of the scene is represented as a hierarchical graph of
		// nodes
		Scene scene = new Scene(rootPane, 300, 300);

		// a button control with event handler to print a message when the
		// button is pressed.
		Button btn = createButton();

		// add the button to the root

		// add scene to the stage

		// Attempts to show this Window by setting visibility to true;


	private Button createButton() {
		Button btn = new Button();
		btn.setText("Say 'Hello World'");
		// when a button is pressed this eventhandler will be invoked and the
		// message will be shown in the console
		btn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				System.out.println("Action to be handled here!");
		return btn;


And here is the output





In case you are using this HelloWorld in eclipse and you get the below warning.
Access restriction: The type Pane is not accessible due to
restriction on required library C:\Program Files\Java\jre8_0\lib\ext\jfxrt.jar

You can install e(fx)clipse bundles. The all in one downloads include e(fx)clipse bundles and addon bundles only available from You can search for e(fx)clipse bundles in eclipse marketplace and install. For more options refer to this stackoverflow question

Like this post? Don’t forget to share it!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.