有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在JavaFX TableView中设置行高

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

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

还有别的办法解决这个问题吗


共 (1) 个答案

  1. # 1 楼答案

    你可以用

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

    .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.

    在本例中,-fx-cell-size是行高(实际上是每个单元格的高度),如Oracle's JavaFX CSS Reference Guide所确认的

    -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.

    我在以下示例中尝试了上述解决方案:

    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();
            }
        }
    }
    

    风格。css:

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