Javafx:绑定Hbox间距?

时间:2017-07-28 08:49:27

标签: java javafx javafx-8 bind spacing

我在表格下面有TableViewHbox,在hBox中有三个Label s包含一个文本,两个包含表中两列的总和。我想为HBox设置一个动态的间距,以便将两个标签正好位于它们所属的表格中的两列之下。是否有可能将HBox的间距绑定到列位置。 我也接受任何其他解决方案,将标签固定在相应列的正下方。 这是一张显示我想要的图片:

enter image description here

1 个答案:

答案 0 :(得分:2)

将每个标签的minWidthprefWidth属性绑定到相应列的width属性:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class TableWithLabelsBelowColumns extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Void> table = new TableView<>();
        table.getItems().add(null);

        TableColumn<Void, Void> firstNameColumn = new TableColumn<>("First Name");
        TableColumn<Void, Void> lastNameColumn = new TableColumn<>("Last Name");
        TableColumn<Void, Void> emailColumn = new TableColumn<>("Email");
        table.getColumns().add(firstNameColumn);
        table.getColumns().add(lastNameColumn);
        table.getColumns().add(emailColumn);

        Label fnLabel = new Label("FN");
        Label lnLabel = new Label("LN");
        Label emailLabel = new Label("E");

        fnLabel.prefWidthProperty().bind(firstNameColumn.widthProperty());
        fnLabel.minWidthProperty().bind(firstNameColumn.widthProperty());
        lnLabel.prefWidthProperty().bind(lastNameColumn.widthProperty());
        lnLabel.minWidthProperty().bind(lastNameColumn.widthProperty());
        emailLabel.prefWidthProperty().bind(emailColumn.widthProperty());
        emailLabel.minWidthProperty().bind(emailColumn.widthProperty());

        HBox labels = new HBox(fnLabel, lnLabel, emailLabel);

        BorderPane root = new BorderPane(table);
        root.setBottom(labels);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

另一种方法是子类Pane并覆盖layoutChildren()方法,以根据列的宽度定位标签。注册一个侦听器,在窗格上请求每个列的宽度布局:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class TableWithLabelsBelowColumns extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Void> table = new TableView<>();
        table.getItems().add(null);

        TableColumn<Void, Void> firstNameColumn = new TableColumn<>("First Name");
        TableColumn<Void, Void> lastNameColumn = new TableColumn<>("Last Name");
        TableColumn<Void, Void> emailColumn = new TableColumn<>("Email");
        table.getColumns().add(firstNameColumn);
        table.getColumns().add(lastNameColumn);
        table.getColumns().add(emailColumn);

        Label fnLabel = new Label("FN");
        Label lnLabel = new Label("LN");
        Label emailLabel = new Label("E");

        Pane labelPane = new Pane(fnLabel, lnLabel, emailLabel) {
            @Override
            protected void layoutChildren() {
                double fnWidth = firstNameColumn.getWidth();
                double fnHeight = fnLabel.prefHeight(fnWidth);
                fnLabel.resizeRelocate(0, 0, fnWidth, fnHeight);

                double lnWidth = lastNameColumn.getWidth();
                double lnHeight = lnLabel.prefHeight(lnWidth);
                lnLabel.resizeRelocate(fnWidth, 0, lnWidth, lnHeight);

                double emailWidth = emailColumn.getWidth();
                double emailHeight = emailLabel.prefHeight(emailWidth);
                emailLabel.resizeRelocate(fnWidth+lnWidth, 0, emailWidth, emailHeight);
            }
        };
        ChangeListener<? super Number> listener = (obs, oldValue, newValue) -> labelPane.requestLayout();
        firstNameColumn.widthProperty().addListener(listener);
        lastNameColumn.widthProperty().addListener(listener);
        emailColumn.widthProperty().addListener(listener);

        BorderPane root = new BorderPane(table);
        root.setBottom(labelPane);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}