简体   繁体   English

在JavaFX TableView中设置行高

[英]Set Row Height in JavaFX TableView

How can I set row Height in JavaFX table View? 如何在JavaFX表View中设置行高? I tried to increase it by using css but this didn't work: 我试图通过使用CSS来增加它,但这不起作用:

.table-row{
    line-height: 50px;
}

Is there any other way to solve this? 有没有其他方法可以解决这个问题?

You can use 您可以使用

.table-row-cell {
    -fx-cell-size: 50px;
}

The .table-row-cell is the class assigned to the table rows, as stated in capian.css , available at jfxrt.jar (com/sun/javafx/scene/control/skin/caspian/caspian.css): .table-row-cell是分配给表行的类,如capian.css ,可在jfxrt.jar(com / sun / javafx / scene / control / skin / caspian / caspian.css)获得:

Each row in the table is a table-row-cell. 表中的每一行都是一个表行单元格。 Inside a table-row-cell is any number of table-cell. 表格 - 单元格内部是任意数量的表格单元格。

And -fx-cell-size , in this case, is the row height (actually, each cell height), as confirmed at Oracle's JavaFX CSS Reference Guide 在这种情况下, -fx-cell-size是行高(实际上,每个单元格高度),如Oracle的JavaFX CSS参考指南中所确认的那样

-fx-cell-size : The cell size. -fx-cell-size :单元格大小。 For vertical ListView or a TreeView or TableView this is the height, for a horizontal ListView this is the width. 对于垂直ListView或TreeView或TableView,这是高度,对于水平ListView,这是宽度。

I tried the solution above on the following example: 我在以下示例中尝试了上面的解决方案:

PetsTable: (Note the use of scene.getStylesheets().add("styles/styles.css"); , defining the css file to be employed) PetsTable :(注意使用scene.getStylesheets().add("styles/styles.css");定义要使用的css文件)

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class PetsTable extends Application {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Pets");
        stage.setWidth(200);
        stage.setHeight(200);

        ObservableList<Pet> data = FXCollections.observableArrayList(new Pet("Kitty", "Susan"), 
                new Pet("Ploft", "Jackob"));
        TableView<Pet> table = new TableView<Pet>();

        TableColumn name = new TableColumn("Name");
        name.setMinWidth(100);
        name.setCellValueFactory(new PropertyValueFactory<Pet, String>("name"));

        TableColumn owner = new TableColumn("Owner");
        owner.setMinWidth(100);
        owner.setCellValueFactory(new PropertyValueFactory<Pet, String>("owner"));

        table.setItems(data);
        table.getColumns().addAll(name, owner);

        ((Group) scene.getRoot()).getChildren().addAll(table);

        /**********************************************/
        /********* Setting the css style file *******/
        /**********************************************/
        scene.getStylesheets().add("styles/styles.css");

        stage.setScene(scene);
        stage.show();
    }

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

    public static class Pet {

        private final SimpleStringProperty name;
        private final SimpleStringProperty owner;

        private Pet(String name, String owner) {
            this.name = new SimpleStringProperty(name);
            this.owner = new SimpleStringProperty(owner);
        }

        public String getName() {
            return name.get();
        }

        public String getOwner() {
            return owner.get();
        }
    }
}

styles.css: styles.css的:

.table-row-cell {
    -fx-cell-size: 50px;
}

Try this if you are using a listview! 如果您使用列表视图,请尝试此操作!

.list-cell {
    -fx-cell-size: 250px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM