在 JavaFX TableView 中设置行高

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18700478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:18:09  来源:igfitidea点击:

Set Row Height in JavaFX TableView

javajavafx-2

提问by Feras Odeh

How can I set row Height in JavaFX table View? I tried to increase it by using css but this didn't work:

如何在 JavaFX 表视图中设置行高?我试图通过使用 css 来增加它,但这不起作用:

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

Is there any other way to solve this?

有没有其他方法可以解决这个问题?

采纳答案by Crferreira

You can use

您可以使用

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

The .table-row-cellis 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 /阳光/ JavaFX的/场景/控制/护肤/里海/ 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. For vertical ListView or a TreeView or TableView this is the height, for a horizontal ListView this is the width.

-fx-cell-size:单元格大小。对于垂直 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", "Hymanob"));
        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:

样式.css:

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

回答by Sedrick

Try this if you are using a listview!

如果您使用的是列表视图,请尝试此操作!

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