יום שישי, 24 במאי 2013

Simple JavaFX view by code

The JavaFX view can be construct by directly code or by fxml descriptor.
The following demo show a simple javaFX form construct by code:
public class JavaFXSimpleSample extends Application {
    
    @Override
    public void start(Stage primaryStage) throws IOException {
    
        final Button pressMeBtn = new Button();
        pressMeBtn.setText("Press me please ");
        pressMeBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                pressMeBtn.setText("You pressed on me!");
            }
        });
        
        StackPane theStackPanel = new StackPane();
        theStackPanel.getChildren().add(pressMeBtn);
        
        Scene scene = new Scene(root, 300, 300);
        
        primaryStage.setTitle("Java Fx view construct form code !");
        primaryStage.setScene(scene);
        primaryStage.show();
    
    }
}
The code result:

Capture10  

The JavaFX platform construct and init the JavaFXSimpleSample class .
That is the main class of the application note that there is no main method to be called.
Capture20 



The start method is invoked with a given stage value . The stage is the main window of the application ,containing the title of the application the frame and the inside area.


The demo control the title of the main window by invoking the setTitle method of the stage object.
The stage object can control the dialog characteristics like if its frame is resizable or fixed .







The Sample construct a javaFX button widget named pressMeBtn.
The pressMeBtn is declared as final in order to be available in events anonymous classes.
 final Button pressMeBtn = new Button();

An event handle for the button click event is declared.
Note that extra events handlers can be declared for example for mouse , keyboard action of the button  Drag and drop etc.
 pressMeBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                pressMeBtn.setText("You pressed on me!");
            }
        });




The pressMeBtn is positioned inside a StackPane widget that is the main panel in the dialog inner area (The scene).
The stackPane is a panel that place its child is a stack structure one above the other  in the Z axis .Note that this panel has deferent meaning then the wpf stackpanel.


The following example demonstrat the javaFX stackPane

 final Button pressMeBtn = new Button();
 pressMeBtn.setText("Press me please ");
  
 final Button pressMeBtn1 = new Button();
 pressMeBtn1.setText("next button is stack ");
        
 final Button pressMeBtn2 = new Button();
 pressMeBtn2.setText("next button is stack ");

 StackPane root = new StackPane();
 root.getChildren().add(pressMeBtn);
 root.getChildren().add(pressMeBtn1);
 root.getChildren().add(pressMeBtn2);
 Scene scene = new Scene(root, 300, 300);



Capture11  

The stage (The all window )is wrap the scene (the inner of the window )so the scene sizes controls the stage (The main window size)

אין תגובות:

הוסף רשומת תגובה