Scene and VBox in JavaFX - ProgrammerTech
Language:

Search

Scene and VBox in JavaFX

  • Share this:
Scene and VBox in JavaFX

Scene section and root VBOX in JavaFX

Section Scene and VBox Root, we will notice that in the rest of the upcoming JavaFX lessons, it is based on the tree hierarchy. I mean, always in the world of technology, you will encounter the term trees or trees, and the tree depends on the attribute of the father and the son, and here your work will be in JavaFX.

  1. Scene definition: It is the segment or container that contains all the elements (buttons, settings, texts, trowels...etc) and comes with the package, meaning all the tools used to build the Stage.
  2. Definition of VBox: It is the node to which the item is added and then VBox is added to Scene.

Example 1

package sajafx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Text;
import javafx.scene.layout.VBox;
public class Sajafx extends Application{   
    public void start(Stage stage) {
        Text msg=new Text("Ahmed Kaissar");
        VBox root=new VBox();
        root.getChildren().add(msg);
        Scene s=new Scene(root,660,450);
        stage.setScene(s);
        stage.show();
    }
     public static void main(String []args){
        launch(args);
     }
}

At the beginning of this code we called the Application library and also called the Scene container package which contains all the elements via scene.Scene . We also called the stage package and the create box package, which follows the parent via scene.text and finally called the root package. And within the start function we put the object is stage and inside it we create the class of the text field that we want to show in the form and the text is Ahmed kaissar. And we created the son class (root) and added the msg in the in via the getChildren function. And we add the son to the father with the model sizing with a width of 660 and a length of 150. We add the object to the model via setScene and at the end of the code we show the model by stage.show.

And when the previous code is run, the form will appear as follows.

create stage javafx vbox

 

Create elements and add them to root JavaFX

How to add more than one element and then add the elements to the root (VBOX) and then add the root to the container (Scene) After that we add the Scene to the final stage. In the previous paragraph, we learned how to add one element, then add it to the root son, and then add the root to Scene. We will now learn how to add two elements and we will learn how to deal with more than one element and add them to the root.

Example 2

package sajafx;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
public class Sajafx extends Application{
    public void start(Stage Stage) {
        Label L1=new Label("Sajom");
        Button B=new Button("Click");
        VBox root=new VBox();
        root.setSpacing(2);
        root.getChildren().addAll(L1,B);
        Scene Scene=new Scene(root,500,300);
        Stage.setScene(Scene);
        Stage.show();
    }
     public static void main(String []args){
        launch(args);
     }
}

We have summoned several packages that we will use within this code, some of which we recognized in the previous lessons, and some of them we consider new. Including the package to add the control.Button button and the package to add the control.Label field. And we entered the start function and the object it has is Stage and we wrote within it the text class, which is Label. The script shown is Sajom and we created the Button classes and the VBox root son classes. And through the setSpacing function, we set the number of children or elements 2 and add these elements to the root using the getChildren function. And we added the son to the father and set the size, width 500, length 300 and added the object to the model and showed it.

And when we execute the previous code, the form will appear in the form.

Create elements and add them to root JavaFX

Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com