gpt4 book ai didi

javafx-2 - 带有多行表头的JavaFX 2.0 Table

转载 作者:行者123 更新时间:2023-12-04 04:46:42 24 4
gpt4 key购买 nike

是否可以使javaFX 2.0表带有多行标题?
我在网上找到的所有示例都有表格,其中columnt header 宽度=文字大小,不包含换行符。屏幕上显示了我所拥有的和所需要的一切:

最佳答案

我想出了以下功能:

private void makeHeaderWrappable(TableColumn col) {
Label label = new Label(col.getText());
label.setStyle("-fx-padding: 8px;");
label.setWrapText(true);
label.setAlignment(Pos.CENTER);
label.setTextAlignment(TextAlignment.CENTER);

StackPane stack = new StackPane();
stack.getChildren().add(label);
stack.prefWidthProperty().bind(col.widthProperty().subtract(5));
label.prefWidthProperty().bind(stack.prefWidthProperty());
col.setGraphic(stack);
}

完整的可执行示例是 in this gist(需要 2.2 developer preview as a minimum)。
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class TableWrappedHeaders extends Application {
public static void main(String[] args) { launch(args); }

@Override public void start(Stage stage) {
TableColumn firstNameCol = new TableColumn("First Name (which is a really long name)");
makeHeaderWrappable(firstNameCol);
firstNameCol.setPrefWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setPrefWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName"));

TableView table = new TableView();
table.getColumns().addAll(firstNameCol, lastNameCol);
table.setItems(FXCollections.observableArrayList(
new Person("Jacob", "Smith"),
new Person("Isabella", "Johnson"),
new Person("Ethan", "Williams")
));
table.setPrefSize(250, 200);

Pane layout = new VBox(10);
layout.setStyle("-fx-padding: 10;");
layout.getChildren().addAll(table);
stage.setScene(new Scene(layout));
stage.show();
}

private void makeHeaderWrappable(TableColumn col) {
Label label = new Label(col.getText());
label.setStyle("-fx-padding: 8px;");
label.setWrapText(true);
label.setAlignment(Pos.CENTER);
label.setTextAlignment(TextAlignment.CENTER);

StackPane stack = new StackPane();
stack.getChildren().add(label);
stack.prefWidthProperty().bind(col.widthProperty().subtract(5));
label.prefWidthProperty().bind(stack.prefWidthProperty());
col.setText(null);
col.setGraphic(stack);
}

public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;

private Person(String fName, String lName) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
}

public String getFirstName() { return firstName.get(); }
public void setFirstName(String fName) { firstName.set(fName); }
public String getLastName() { return lastName.get(); }
public void setLastName(String fName) { lastName.set(fName); }
}
}

可能有更好的方法来执行此操作,但是上面的函数至少对我的测试用例有用。

我认为这可以通过简单的CSS样式实现,但是我无法仅通过CSS来实现。

关于javafx-2 - 带有多行表头的JavaFX 2.0 Table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10952111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com