Saturday, December 10, 2016

Adding a screen

If you read a book about JavaFX, you will find the Scene class. This class is a container type that holds objects displayed inside the Stage. The object of Scene contains another container : Group. The object of Group binds another objects. Those objects are visible in JavaFX Application. Now consider this :

  1. We watch a movie titled The Silence of the Lambs. 
  2. It has an act of conversation between Hannibal Lecter and Clarice Starling. The act is in maximum security jail room. 
  3. This room has a thick bullet proof partition glass, two chairs, a bed several sketch drawings. In this room we have 2 persons : Hannibal Lecter and Clarice Starling.
The movie is analogous to the Stage class. The conversation between Hannibal Lecter and Clarice Starling is analogous to Scene. Finally, things displayed in the scene are analogous to the Group that contains many scene objects int conversation scene.

Now we have an application that display the Hello World Text :


package javafxhelloworld;

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


/**
 *
 * @author Joko Adianto
 */
public class JavaFXHelloWorld extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("Hello World");        
        Group group = new Group();
        group.getChildren().add(label);        
        Scene scene = new Scene(group, 300, 250);        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

The screenshot for this application is depicted below



The container Group holds another objects. In the example above it holds a Label. The object label contains a text value. getChildren().add(l...) is a statement for binding objects to a particular Group.

Friday, December 9, 2016

JavaFX : Basic Framework

Every JavaFX program must adhere to its minimum requirement. At the very basic it comprises of :
  • the library of javafx.application.Application and javafx.stage.Stage
  • It is a class extends or inherits from Application
  • Inside the class there is method overriding : start()
  • An object of stage


  • Remember those 4 requirements in order to be able to run a JavaFX apllication. The very first program is written below :

    package javafxfirstapplication;

    import javafx.application.Application;
    import javafx.stage.Stage;

    /**
     *
     * @author Joko Adianto
     */
    public class JavaFXFirstApplication extends Application {
        
        @Override
        public void start(Stage primaryStage) {
            primaryStage.setTitle("JavaFX : First Application");        
            primaryStage.show();
        }

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
        
    }

    The picture below is the screenshot of the above program




    Here are four exercises to write a JavaFX program that has title of

    JavaFX : Second application
    ....
    ....
    @Override
        public void start(Stage primaryStage) {
            primaryStage.setTitle("JavaFX : Second Application");        
            primaryStage.show();
        }
    ....
    ....

    JavaFX : Third application
    ....
    ....
    @Override
        public void start(Stage primaryStage) {
            primaryStage.setTitle("JavaFX : Third Application");        
            primaryStage.show();
        }
    ....
    ....

    JavaFX : Fourth application
    ....
    ....
    @Override
        public void start(Stage primaryStage) {
            primaryStage.setTitle("JavaFX : Fourth Application");        
            primaryStage.show();
        }
    ....
    ....

    JavaFX : Fifth application
    ....
    ....
    @Override
        public void start(Stage primaryStage) {
            primaryStage.setTitle("JavaFX : Fifth Application");        
            primaryStage.show();
        }
    ....
    ....

    I think its better to rewrite those program, than just "Copy & Paste"ing.