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
Any idea what I need to do?
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.
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.
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.
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With