자식 요소 크기에 대한 GridPane에 수신기를 추가하고 자식 요소 목록의 크기를 기반으로 자식 노드의 레이아웃 제약 조건을 업데이트 할 수 있습니다.

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.collections.ObservableList;
import javafx.geometry.*;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.util.stream.IntStream;
public class GridViewer extends Application {
@Override
public void start(final Stage stage) throws Exception {
ChartGrid grid = new ChartGrid();
grid.getChildren().setAll(
IntStream.range(1, 5)
.mapToObj(this::createLineChart)
.toArray(Chart[]::new)
);
stage.setScene(new Scene(grid));
stage.show();
}
class ChartGrid extends GridPane {
ChartGrid() {
setHgap(5);
setVgap(5);
setPadding(new Insets(5));
setPrefSize(500, 500);
Bindings.size(getChildren()).addListener((observable, oldSize, newSize) -> {
ObservableList<Node> nodes = getChildren();
for (int i = 0; i < newSize.intValue(); i++) {
GridPane.setConstraints(
nodes.get(i),
i/2, i % 2,
1, 1,
HPos.CENTER, VPos.CENTER,
Priority.ALWAYS, Priority.ALWAYS
);
}
});
}
}
private Chart createLineChart(int idx) {
NumberAxis xAxis = new NumberAxis();
NumberAxis yAxis = new NumberAxis();
LineChart<Number, Number> chart = new LineChart<>(xAxis, yAxis);
chart.setTitle("Chart " + idx);
chart.setMinSize(0, 0);
return chart;
}
public static void main(String[] args) throws Exception {
launch(args);
}
}
참고 :이 솔루션의 중요한 부분이 차트의 최소 매개 변수를 설정하는 것입니다 0 :
chart.setMinSize(0, 0);
이 때문에 당신의 가능한 영역의 크기를하는 경우 GridPane이 차트의 최소 크기 (기본적으로 0이 아님)의 합보다 작 으면 차트가 서로 겹치기 시작합니다. 이는 원하는 것이 아닙니다.
상기 창 너비를 2로 나누고 높이를 같게 할 수 있습니까? 빠른 Google 발견 [this] (http://stackoverflow.com/questions/12643125/getting-the-width-and-height-of-the-center-space-in-a-borderpane-javafx-2) – Taelsin
예 나는 이것을 시도했지만 결과는 만족스럽지도 매우 우아하지도 않다. 그리드에 4 개의 차트가있을 때 모든 사용자의 prefWidth/Height를 GridPane의 크기의 절반으로 설정하려고했습니다. 어떻게 든 차트는 여전히 크기가 다르며 차트 1이 가장 크고 차트 4가 가장 작습니다./ –