How to Add a Title to a Window Using JavaFX

Java


In this article, we show how to add a title to a window using JavaFX.

To add a title to a window in JavaFX, the setTitle() function is used. Then inside of this function, you just have to add in double quotations the title that you want the window to be.

The Java code to set the title of the window is shown below.



So in order to start a JavaFX application, you have to import several javafx packages. These include definitions of objects, functions, etc. that is needed to create a JavaFX application.

The class name that you choose must extend Application. Thus, the class inherits everything from the Application class. In this Application class is a broad range of functions and tools needed to create a JavaFX application.

Inside of this application is a method named start().

What we do in our code is we want to override this start() method. This way, we can create our own custom window in JavaFX. The start() method can be seen as the heart of the JavaFX application because it is inside of this method, we add in all the code needed for the JavaFX window.

This start() method takes in the parameter, primaryStage.

The primaryStage represents the window, essentially. So it represents the window object.

To add a title to this stage (window), we take primaryStage follow it with a dot and follow it with setTitle("My First JavaFX GUI"). This add the title, "My First JavaFX GUI" to the window.

Of course, this just add a window. Inside, there is no content. We haven't set a size for the window or anything. This is done simply for the purpose of showing how to add a title to a window with JavaFX.

Running the code above produces the following window: JavaFX Window Title.


Related Resources

. How to Check if a File Exists Using Java




HTML Comment Box is loading comments...