[英]javafx gridpane center align and justify all labels
這個想法是創建一個提交表單,該表單具有許多居中且合理的標簽,例如:
標簽1是客戶名稱 ,標簽2是產品名稱我想做的是將所有標簽水平居中對齊,這很容易,但同時我希望它們對齊。 簡而言之, P應該恰好在第二行網格中的C之下。
請注意,每列中最寬的節點負責確定起始x坐標。 如果節點居中,則需要將所有節點的寬度向左移動最大寬度的一半,這可以使用translateX
屬性實現:
例
@Override
public void start(Stage primaryStage) {
GridPane gp = new GridPane();
ColumnConstraints constraints = new ColumnConstraints();
constraints.setHalignment(HPos.CENTER);
constraints.setPercentWidth(50);
gp.getColumnConstraints().addAll(constraints, constraints);
Random random = new Random();
Node[][] elements = new Node[2][10];
for (int x = 0; x < elements.length; x++) {
final Node[] column = elements[x];
InvalidationListener sizeListener = o -> {
// determine max width
double maxSize = 0;
for (Node n : column) {
double width = n.getLayoutBounds().getWidth();
if (width > maxSize) {
maxSize = width;
}
}
// adjust translate
for (Node n : column) {
n.setTranslateX((n.getLayoutBounds().getWidth() - maxSize) / 2);
}
};
// fill column with strings of random lenght
for (int y = 0; y < 10; y++) {
int charCount = random.nextInt(30);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < charCount; i++) {
sb.append('a');
}
Label text = new Label(sb.toString());
text.layoutBoundsProperty().addListener(sizeListener);
column[y] = text;
}
gp.addColumn(x, column);
sizeListener.invalidated(null);
}
Scene scene = new Scene(gp);
primaryStage.setScene(scene);
primaryStage.show();
}
即使你不能做到這一點與SceneBuilder,你可以應用這種對齊到GridPane
從FXML,如果實現在一個工具類添加此功能,並使用該實用工具類從FXML邏輯。
public final class WeirdAlign {
private WeirdAlign() {
}
public static final Predicate<Node> ALL_PREDICATE = n -> true;
public static void setCombinedCenterJustify(GridPane gridPane, Predicate<Node> predicate) {
InvalidationListener listener = o -> {
Node n = (Node) ((ReadOnlyProperty) o).getBean();
updateGridPaneColumn(gridPane, getGridPaneColumn(n), predicate);
};
ObservableList<Node> children = gridPane.getChildren();
for (Node n : children) {
if (predicate.test(n)) {
n.layoutBoundsProperty().addListener(listener);
}
}
int[] columns = children.stream().filter(predicate).mapToInt(WeirdAlign::getGridPaneColumn).distinct().toArray();
for (int i : columns) {
updateGridPaneColumn(gridPane, i, predicate);
}
children.addListener((ListChangeListener.Change<? extends Node> c) -> {
Set<Integer> columnsToUpdate = new HashSet<>();
while (c.next()) {
if (c.wasRemoved()) {
for (Node n : c.getRemoved()) {
if (predicate.test(n)) {
n.layoutBoundsProperty().removeListener(listener);
columnsToUpdate.add(getGridPaneColumn(n));
}
}
}
if (c.wasAdded()) {
for (Node n : c.getAddedSubList()) {
if (predicate.test(n)) {
n.layoutBoundsProperty().addListener(listener);
columnsToUpdate.add(getGridPaneColumn(n));
}
}
}
}
for (Integer i : columnsToUpdate) {
updateGridPaneColumn(gridPane, i, predicate);
}
});
}
/**
* This method is only here for FXMLLoader.
*/
public static Predicate<Node> getCombinedCenterJustify(GridPane node) {
throw new UnsupportedOperationException();
}
public static void updateGridPaneColumn(GridPane gridPane, int column, Predicate<Node> predicate) {
double maxSize = 0;
for (Node child : gridPane.getChildren()) {
if (column == getGridPaneColumn(child) && predicate.test(child)) {
double width = child.getLayoutBounds().getWidth();
if (width > maxSize) {
maxSize = width;
}
}
}
for (Node child : gridPane.getChildren()) {
if (column == getGridPaneColumn(child) && predicate.test(child)) {
child.setTranslateX((child.getLayoutBounds().getWidth() - maxSize) / 2);
}
}
}
public static int getGridPaneColumn(Node node) {
Integer c = GridPane.getColumnIndex(node);
return c == null ? 0 : c;
}
}
<GridPane xmlns:fx="http://javafx.com/fxml/1" id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" >
<columnConstraints>
<ColumnConstraints fx:id="c1" fillWidth="false" halignment="CENTER" percentWidth="50.0" />
<fx:reference source="c1"/>
</columnConstraints>
<children>
<Label text="abfkaenlgen" />
<Label text="z7492z4z58z4zu uzu53 9zu59h 5n 54uhn" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="bbdnfkbner" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="hhhhhddddssaaeeeaaae" GridPane.rowIndex="1" />
<Label text="gjnwkeibrgbawhgbreökbrgesöbrgsnrgs" GridPane.rowIndex="2" />
</children>
<WeirdAlign.combinedCenterJustify>
<WeirdAlign fx:constant="ALL_PREDICATE"/>
</WeirdAlign.combinedCenterJustify>
</GridPane>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.