10

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?

Mahendra Gunawardena
  • 1,956
  • 5
  • 26
  • 45
Alex
  • 642
  • 1
  • 9
  • 23

2 Answers2

2

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

SedJ601
  • 12,173
  • 3
  • 41
  • 59
  • This still has the same problem though. If you resize the window there will be excess space along the right hand margin. – Alex May 02 '17 at 21:50
  • Just updated code. I am not sure what you are asking now? – SedJ601 May 02 '17 at 21:54
  • In a flow chart there should be extra space to the right up until that space can be filled by the first node on the next row. – SedJ601 May 02 '17 at 21:55
  • Try this with your Insets -> new Insets(100, 0, 30, 30). I think that's as good as it gets. Zero for the right inset. – SedJ601 May 02 '17 at 21:59
  • Thanks man. It's not perfect, but it works for me. I'm really not sure there's any way to do what I want with a flow pane, so this is close enough. – Alex May 02 '17 at 22:14
0

You could use the workaround:

final FlowPane flowPane = new FlowPane() {
    {
        setAlignment(Pos.TOP_CENTER);
    }

    @Override
    protected void layoutChildren() {
        super.layoutChildren();

        final LinkedHashMap<Double, List<Node>> rows = new LinkedHashMap<>();
        getChildren().forEach(node -> {
            final double layoutY = node.getLayoutY();
            List<Node> row = rows.get(node.getLayoutY());
            if (row == null) {
                row = new ArrayList<>();
                rows.put(layoutY, row);
            }

            row.add(node);
        });
            
        final Object[] keys = rows.keySet().toArray();
        final List<Node> firstRow = rows.get(keys[0]);
        final List<Node> lastRow = rows.get(keys[keys.length - 1]);

        for (int i = 0; i < lastRow.size(); i++) {
            lastRow.get(i).setLayoutX(firstRow.get(i).getLayoutX());
        }
    }
};
0009laH
  • 1,960
  • 13
  • 27
alexkishko
  • 61
  • 5