gpt4 book ai didi

java - 在 JavaFX TableView 中设置行高

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:34:34 32 4
gpt4 key购买 nike

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

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

还有什么办法可以解决吗?

最佳答案

你可以使用

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

关于java - 在 JavaFX TableView 中设置行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18700478/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com