向 JavaFx tableView 添加一个简单的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39366828/
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-11 21:04:50  来源:igfitidea点击:

Add a simple row to JavaFx tableView

javajavafxtableviewrow

提问by Danial Kosarifa

I am quite new to TableViewand customizing it in JavaFx. I've gone through many tutorial, I've understood some but stucked in adding rows into my table. For example, I have a table called Table1. According to my understanding I can create columns ArrayListand:

TableView对 JavaFx非常陌生并对其进行了自定义。我经历了很多教程,我已经理解了一些,但仍然坚持在我的表格中添加行。例如,我有一个名为Table1. 根据我的理解,我可以创建列ArrayList并且:

for (int i = 0; i <= columnlist.size() - 1; i++) {
    TableView col = new TableView(columnlist.get(i));
    Table1.getColumns().add(col);
}

Now how can I add a row to this? I will appreciate if you can give me a very simple example as I have gone through other examples but they were too complex :)

现在我如何添加一行呢?如果您能给我一个非常简单的例子,我将不胜感激,因为我已经看过其他例子,但它们太复杂了:)

采纳答案by Dth

You can't create your columns this way. TableViewconstructor takes an ObservableListas its parameter, but it expects to find there table values, in other words, your rows.

您不能以这种方式创建列。TableView构造函数将 anObservableList作为其参数,但它希望找到表值,换句话说,您的行。

I'm afraid that there isn't any generic way to add items to your table, because each table is more or less coupled to its data model. Let's say that you have a Personclass which you want to display.

恐怕没有任何通用的方法可以将项目添加到表中,因为每个表或多或少都与其数据模型耦合。假设您有一个Person要显示的类。

public class Person {
    private String name;
    private String surname;

    public Person(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }
}

In order to do that you'll have to create the table and its two columns. PropertyValueFactorywill fetch the necessary data from your object, but you have to make sure that your fields have an accessor method that follows the standard naming convention (getName(), getSurname(), etc). Otherwise it will not work.

为此,您必须创建表及其两列。PropertyValueFactory将你的对象获取必要的数据,但你必须确保你的字段有遵循标准的命名约定(存取方法getName()getSurname()等等)。否则它将无法工作。

TableView tab = new TableView();

TableColumn nameColumn = new TableColumn("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));

TableColumn surnameColumn = new TableColumn("Surname");
surnameColumn.setCellValueFactory(new PropertyValueFactory<>("surname"));

tab.getColumns().addAll(nameColumn, surnameColumn);

Now all you have to do is to create your Personobject and add it to the table items.

现在您要做的就是创建您的Person对象并将其添加到表项中。

Person person = new Person("John", "Doe");
tab.getItems().add(person);