Javafx:绑定 Hbox 间距?
Javafx: bind Hbox spacing?
我在table下面有一个TableView
和一个Hbox
,在hBox中有三个Label
,一个包含文本,两个包含总和table 中的两列。我想以某种方式为 HBox 设置一个动态间距,以使两个标签恰好在它们所属的 table 中的两列下方对齐。是否有可能将 HBox
的间距绑定到列位置。
我也接受将标签固定在相应列下方的任何其他解决方案。
这是一张显示我想要的图像:
将每个标签的 minWidth
和 prefWidth
属性绑定到相应列的 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);
}
}
我在table下面有一个TableView
和一个Hbox
,在hBox中有三个Label
,一个包含文本,两个包含总和table 中的两列。我想以某种方式为 HBox 设置一个动态间距,以使两个标签恰好在它们所属的 table 中的两列下方对齐。是否有可能将 HBox
的间距绑定到列位置。
我也接受将标签固定在相应列下方的任何其他解决方案。
这是一张显示我想要的图像:
将每个标签的 minWidth
和 prefWidth
属性绑定到相应列的 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);
}
}