是否可以将JavaFX TableColumn的标题文本转换为另一列?

时间:2014-04-16 17:28:12

标签: css layout javafx

我有一个带有两个TableColumns的TableView。左侧是一个宽列,需要能够显示最多30个字符的条目。右边是一个窄列,需要只能显示一个字符。但是,右栏的标题需要很长一段时间(这是必需的)。但是,我没有足够的空间来拥有一个像所需标题文本一样胖的列。如果我将右列与其标题文本一样宽,则左列中的数据将被截断。我希望右侧列的标题文本渗透到左侧的列中(不用担心它会进入左列的标题文本)。这可以通过设置我的TableView的一些fx css和/或一个或两个TableColumns来实现吗?

1 个答案:

答案 0 :(得分:3)

这是一个示例,其中列标题“Long Last Name”渗透到其左侧的列标题中。

bleed

bleed.css

.column-header.left-header > .label {
    -fx-alignment: baseline-left;
}

.column-header.bleed-header > .label {
    -fx-alignment: baseline-right;
    -fx-padding: 0 3 0 0;
}

启用“出血”的代码段

TableColumn<Person, String> lastNameCol = new TableColumn<>();
lastNameCol.setMinWidth(80);
lastNameCol.setCellValueFactory(
        new PropertyValueFactory<>("lastName"));
lastNameCol.getStyleClass().add("bleed-header");

Label headerLabel = new Label("Long Last Name");
headerLabel.setMinWidth(Control.USE_PREF_SIZE);
lastNameCol.setGraphic(headerLabel);

解决方案需要一些技巧:

  1. 使用css设置对齐属性。
  2. 使用标题的标签图形。
  3. 提供标签图形可让您直接控制最小宽度(以防止标题标签过长时删除标题标签的默认行为。)

    可执行代码示例

    import javafx.application.Application;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.collections.*;
    import javafx.geometry.Insets;
    import javafx.scene.*;
    import javafx.scene.control.*;
    import javafx.scene.control.cell.PropertyValueFactory;
    import javafx.scene.layout.VBox;
    import javafx.scene.text.Font;
    import javafx.stage.Stage;
    
    public class TableViewHeaderBleed extends Application {
    
        private TableView<Person> table = new TableView<>();
        private final ObservableList<Person> data =
            FXCollections.observableArrayList(
                new Person("Jacob", "Smith", "jacob.smith@example.com"),
                new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                new Person("Ethan", "Williams", "ethan.williams@example.com"),
                new Person("Emma", "Jones", "emma.jones@example.com"),
                new Person("Michael", "Brown", "michael.brown@example.com")
            );
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage stage) {
            Scene scene = new Scene(new Group());
            stage.setTitle("Table View Sample");
    
            table.setPrefHeight(200);
    
            final Label label = new Label("Address Book");
            label.setFont(new Font("Arial", 20));
    
            TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
            firstNameCol.setMinWidth(100);
            firstNameCol.setCellValueFactory(
                    new PropertyValueFactory<>("firstName"));
            firstNameCol.getStyleClass().add("left-header");
    
            TableColumn<Person, String> lastNameCol = new TableColumn<>();
            lastNameCol.setMinWidth(80);
            lastNameCol.setCellValueFactory(
                    new PropertyValueFactory<>("lastName"));
            lastNameCol.getStyleClass().add("bleed-header");
    
            Label headerLabel = new Label("Long Last Name");
            headerLabel.setMinWidth(Control.USE_PREF_SIZE);
            lastNameCol.setGraphic(headerLabel);
    
            TableColumn<Person, String> emailCol = new TableColumn<>("Email");
            emailCol.setMinWidth(100);
            emailCol.setCellValueFactory(
                    new PropertyValueFactory<>("email"));
    
            table.setItems(data);
            table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
    
            final VBox vbox = new VBox();
            vbox.setSpacing(5);
            vbox.setPadding(new Insets(10));
            vbox.getChildren().addAll(label, table);
    
            ((Group) scene.getRoot()).getChildren().addAll(vbox);
    
            scene.getStylesheets().add(getClass().getResource("bleed.css").toExternalForm());
    
            stage.setScene(scene);
            stage.show();
        }
    
        public static class Person {
            private final SimpleStringProperty firstName;
            private final SimpleStringProperty lastName;
            private final SimpleStringProperty email;
    
            private Person(String fName, String lName, String email) {
                this.firstName = new SimpleStringProperty(fName);
                this.lastName = new SimpleStringProperty(lName);
                this.email = new SimpleStringProperty(email);
            }
    
            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);
            }
    
            public String getEmail() {
                return email.get();
            }
    
            public void setEmail(String fName) {
                email.set(fName);
            }
       }
    }