First Java FX Application

Prerequisites for starting to develop JavaFX Application:

– Make sure that java 1.7 is specified as project SDK in your IDE.

– If you are using java 1.6, add jfxrt.jar as a dependency to the project.

Mandatory steps involved in creating an application:

– Create a Java class and extend it with Application abstract class.

– Implement start method which gets invoked when the application is started.

– Implement a main method and invoke launch method

– Provide the implementation inside start method

Implementation:

– Create the nodes which need to displayed on user interface

– Create a layout manager such as Border Pane

– Add the nodes to the layout manager

– Create a scene and specify layout manager as the root of scene graph

– Set the scene on primary stage provided by application

– Show the stage such that the stage is visible with computed preferred size

Example:


public class JavaFXApplication extends Application
{
   public static final String TITLE = "Java FX Application";
   private Button closeButton = new Button("Close");

   @Override
   public void start(Stage stage) throws Exception
   {
      initializeStage(stage);

      closeButton.setPrefSize(50, 25);

      BorderPane root = new BorderPane();
      root.setCenter(closeButton);

      stage.setScene(new Scene(root, 400, 300));
      stage.show();
    }

    private void initializeStage(Stage stage)
    {
       stage.setTitle(TITLE);
    }

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s