Create x Using JavaFX



Once you create all the required nodes for your application you can arrange them using a layout. Where a layout is a process of calculating the position of objects in the given space. JavaFX provides various layouts in the javafx.scene.layout package.

x

In this layout, the nodes are arranged in a single horizontal row. You can create an x in your application by instantiating the javafx.scene.layout.x class. You can set the padding around the x using the setPadding() method.

To add nodes to this pane you can either pass them as arguments of the constructor or, add them to the observable list of the pane as −

getChildren().addAll();

Example

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.x;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class xExample extends Application {
   public void start(Stage stage) {
      //Creating labels
      Label label1 = new Label("User Name: ");
      Label label2 = new Label("Password: ");
      //Creating text and password fields
      TextField textField = new TextField();
      PasswordField pwdField = new PasswordField();
      //Adding labels for nodes
      x box = new x(5);
      box.setPadding(new Insets(50, 5 , 5, 50));
      box.getChildren().addAll(label1, textField, label2, pwdField);
      //Setting the stage
      Scene scene = new Scene(box, 595, 150, Color.BEIGE);
      stage.setTitle("x Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 2020-05-19T13:09:29+05:30

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started