Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: Aligning all content in a FlowPane

I'm working on something that stores a grid of tiles and I'm using a FlowPane so that the tiles can flexibly wrap around if the window is resized.

The problem I have is that there's usually a lot of excess space on the right hand side of the screen, and I would like to distribute it evenly on both sides. Setting the alignment to center kind of works, but it centers the contents in each row, and I want each row to start flush on the left hand side?

Visualization of what I'm talking about:

FlowPane alignment

FlowPane alignment

Any idea what I need to do?

like image 438
Alex Avatar asked May 02 '17 21:05

Alex


People also ask

How does Flowpane work in JavaFX?

FlowPane lays out its children in a flow that wraps at the flowpane's boundary. A horizontal flowpane (the default) will layout nodes in rows, wrapping at the flowpane's width. A vertical flowpane lays out nodes in columns, wrapping at the flowpane's height.

What is the difference between a Flowpane and a VBox or HBox why would you use one over the other?

FlowPane – lays out its children in a flow that wraps at the flowpane's boundary. HBox – arranges its content nodes horizontally in a single row. VBox – arranges its content nodes vertically in a single column. AnchorPane – anchor nodes to the top, bottom, left side, or center of the pane.

What is the difference between HBox and VBox pane?

HBox is a subclass of Pane that lays out its children next to each other, in a horizontal row. VBox is a subclass of Pane that lays out its children in vertical column. An HBox might be used as the bottom component in a BorderPane, making it possible to line up several components along the bottom of the border pane.

What is StackPane in JavaFX?

StackPane lays out its children in a back-to-front stack. The z-order of the children is defined by the order of the children list with the 0th child being the bottom and last child on top. If a border and/or padding have been set, the children will be layed out within those insets.


1 Answers

You can adjust the borders to get a closer output.

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication95 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        FlowPane flowPane = new FlowPane();

        List<Circle> circleContainer = new ArrayList();
        for(int i = 0; i < 5; i++)
        {
            Circle c1 = new Circle();
            c1.setRadius(50);
            c1.setFill(Color.BLUE);
            circleContainer.add(c1);
        }

        flowPane.getChildren().addAll(circleContainer);
//        flowPane.maxHeight(500);
//        flowPane.setMaxWidth(300);
        flowPane.setPadding(new Insets(30, 30, 30, 30));
        StackPane root = new StackPane();
        root.getChildren().add(flowPane);

        Scene scene = new Scene(root, 600, 400);


        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

enter image description here enter image description here

like image 200
Sedrick Avatar answered Oct 14 '22 17:10

Sedrick