Tuesday 10 November 2020

JavaFX: VBox layout

VBox lays out its children in a single vertical column and it doesn’t wrap content at boundaries.

 

What is the behavior, if vbox is resized larger than its preferred height?

If a vbox is resized larger than its preferred height, by default it will keep children to their preferred heights, leaving the extra space unused. If an application wishes to have one or more children be allocated that extra space it may optionally set a vgrow constraint on the child.

 

VBox class provides following constructors to get an instance of VBox.

 

public VBox()

public VBox(double spacing)

public VBox(Node... children)

public VBox(double spacing, Node... children)

spacing: specifies the amount of horizontal space between each child

children: specifies the initial set of children for this pane.

 

Find the below working application.

 

vBoxDemo.fxml

<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.effect.*?>

<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.layout.controller.VBoxController"
	fx:id="vBox1" spacing="20" alignment="center">

	<Rectangle fx:id="rectangle1" 
		fill="salmon"
		width="150" height="100" 
	 />

		
	<Circle fx:id="circle1" 
		radius="50" 
		fill="green"/>
	
	<Ellipse fx:id="ellipse1" 
		radiusX ="100" radiusY="50" 
		fill="yellow"/>
	
</VBox>

 

VBoxController.java

package com.sample.app.layout.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;

public class VBoxController implements Initializable {

	@FXML
	private Circle circle1;

	@FXML
	private Rectangle rectangle1;

	@FXML
	private Ellipse ellipse1;

	@FXML
	private VBox vBox1;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		DropShadow dropShadow = new DropShadow();
		dropShadow.setOffsetX(5);
		dropShadow.setOffsetY(5);
		dropShadow.setColor(Color.GRAY);

		vBox1.setEffect(dropShadow);
	}

}

 

VBoxDemo.java

package com.sample.app.layout;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class VBoxDemo extends Application {

	private static final String FXML_FILE = "/vboxDemo.fxml";
	private static final String STAGE_TITLE = "VBox demo";

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

	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));

		Scene scene = new Scene(root, 300, 500, Color.LIGHTGRAY);

		primaryStage.setTitle(STAGE_TITLE);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

 

Output

 


 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment