Setting row height in JavaFX TableView

How to set row height in JavaFX View table? I tried to increase it using css, but this did not work:

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

Is there any other way to solve this problem?

+6
source share
2 answers

you can use

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

.table-row-cell is the class assigned to the table rows as specified in capian.css , available in jfxrt.jar (com / sun / javafx / scene / control / skin / caspian / caspian.css):

Each row in the table is a row-cell-table. Inside the table-row-cell there is any number of the table-cell.

And -fx-cell-size , in this case, is the row height (in fact, each cell height), as confirmed in the Oracle JavaFX CSS Reference

-fx-cell-size : cell size. For a vertical ListView or TreeView or TableView, this is the height; for a horizontal ListView, this is the width.

I tried the solution above in the following example:

PetsTable: (note the use of scene.getStylesheets().add("styles/styles.css"); which defines the css file to use)

 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:

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

Try this if you are using listview!

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

Source: https://habr.com/ru/post/953494/


All Articles