java 在 TableView Javafx 中插入数据

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

Inserting data in a TableView Javafx

javajavafx-2javafx

提问by Victor Laerte

I'm trying to insert data into a Javafx TableView, actually I did it, but it fills the row with the following String:

我正在尝试将数据插入 Javafx TableView,实际上我做到了,但它使用以下字符串填充行:

IntegerProperty [value: 72] and etc...

IntegerProperty [value: 72] 等...

How can I show only the value fill in my rows??

如何仅在我的行中显示值填充?

My TableView code:

我的 TableView 代码:

@FXML TableView tableView = new TableView<MetaDadosInfo>();
@FXML javafx.scene.control.TableColumn instituicaoCol;
@FXML javafx.scene.control.TableColumn anoCol;
@FXML javafx.scene.control.TableColumn tamanhoCol;
@FXML javafx.scene.control.TableColumn tipoCol;
@FXML javafx.scene.control.TableColumn nomeCol;

final ObservableList<MetaDadosInfo> data = FXCollections.observableArrayList(
    new MetaDadosInfo(codigoInstituicao, ano, size, type, name));

instituicaoCol.setCellValueFactory(
        new PropertyValueFactory<MetaDadosInfo, String>("codigoInstituicao"));

anoCol.setCellValueFactory(
        new PropertyValueFactory<MetaDadosInfo, String>("ano"));

tamanhoCol.setCellValueFactory(
        new PropertyValueFactory<MetaDadosInfo, String>("size"));

tipoCol.setCellValueFactory(
        new PropertyValueFactory<MetaDadosInfo, String>("type"));

nomeCol.setCellValueFactory(
        new PropertyValueFactory<MetaDadosInfo, String>("name"));

tableView.setItems(data);

MetaDadosInfo class:

MetaDadosInfo 类:

public class MetaDadosInfo {
    private SimpleIntegerProperty codigoInstituicao;
    private SimpleIntegerProperty ano;
    private SimpleLongProperty size;
    private SimpleStringProperty type;
    private SimpleStringProperty name;

    public MetaDadosInfo(int codigoInstituicao, int ano, long size, String type, String name) {
        this.codigoInstituicao = new SimpleIntegerProperty (codigoInstituicao);
        this.ano = new SimpleIntegerProperty (ano);
        this.size = new SimpleLongProperty (size);
        this.type = new SimpleStringProperty (type);
        this.name = new SimpleStringProperty (name);
    }

    public SimpleIntegerProperty getCodigoInstituicao() {
        return codigoInstituicao;
    }

    public void setCodigoInstituicao(SimpleIntegerProperty codigoInstituicao) {
        this.codigoInstituicao = codigoInstituicao;
    }

    public SimpleIntegerProperty getAno() {
        return ano;
    }

    public void setAno(SimpleIntegerProperty ano) {
        this.ano = ano;
    }

    public SimpleLongProperty getSize() {
        return size;
    }

    public void setSize(SimpleLongProperty size) {
        this.size = size;
    }

    public SimpleStringProperty getType() {
        return type;
    }

    public void setType(SimpleStringProperty type) {
        this.type = type;
    }

    public SimpleStringProperty getName() {
        return name;
    }

    public void setName(SimpleStringProperty name) {
        this.name = name;
    }

}

回答by Victor Laerte

The error was in getters and setters from my MetaDadosInfo class, the right way is:

错误出在我的 MetaDadosInfo 类的 getter 和 setter 中,正确的方法是:

public class MetaDadosInfo {
    private SimpleIntegerProperty codigoInstituicao;
    private SimpleIntegerProperty ano;
    private SimpleLongProperty size;
    private SimpleStringProperty type;
    private SimpleStringProperty name;

    public MetaDadosInfo(int codigoInstituicao, int ano, long size, String type, String name) {
        this.codigoInstituicao = new SimpleIntegerProperty (codigoInstituicao);
        this.ano = new SimpleIntegerProperty (ano);
        this.size = new SimpleLongProperty (size);
        this.type = new SimpleStringProperty (type);
        this.name = new SimpleStringProperty (name);
    }

    public int getCodigoInstituicao() {
        return codigoInstituicao.get();
    }

    public void setCodigoInstituicao(int codigoInstituicao) {
        this.codigoInstituicao.set(codigoInstituicao);
    }

    public int getAno() {
        return ano.get();
    }

    public void setAno(int ano) {
        this.ano.set(ano);
    }

    public Long getSize() {
        return size.get();
    }

    public void setSize(long size) {
        this.size.set(size);
    }

    public String getType() {
        return type.get();
    }

    public void setType(String type) {
        this.type.set(type);
    }

    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        this.name.set(name);
    }

}

回答by Ranjeet Rana

After wasting my day i finally able to find the solution in very easy way

在浪费了我的一天之后,我终于能够以非常简单的方式找到解决方案

package test;

import java.util.HashMap;
import java.util.Map;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

public class Test extends Application {

    public static final String Column1MapKey = "A";
    public static final String Column2MapKey = "B";

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(300);
        stage.setHeight(500);
        final Label label = new Label("Student IDs");
        label.setFont(new Font("Arial", 20));
        TableColumn<Map, String> firstDataColumn = new TableColumn<>("Class A");
        TableColumn<Map, String> secondDataColumn = new TableColumn<>("Class B");
        firstDataColumn.setCellValueFactory(new MapValueFactory(Column1MapKey));
        firstDataColumn.setMinWidth(130);
        secondDataColumn.setCellValueFactory(new MapValueFactory(Column2MapKey));
        secondDataColumn.setMinWidth(130);
        TableView table_view = new TableView<>();
        table_view.setItems(generateDataInMap());
        table_view.setEditable(true);
        table_view.getSelectionModel().setCellSelectionEnabled(true);
        table_view.getColumns().setAll(firstDataColumn, secondDataColumn);


//        Callback<TableColumn<Map, String>, TableCell<Map, String>> cellFactoryForMap = new Callback<TableColumn<Map, String>, TableCell<Map, String>>() {
//            @Override
//            public TableCell call(TableColumn p) {
//                return new TextFieldTableCell(new StringConverter() {
//                    @Override
//                    public String toString(Object t) {
//                        return t.toString();
//                    }
//
//                    @Override
//                    public Object fromString(String string) {
//                        return string;
//                    }
//                });
//            }
//        };
//        firstDataColumn.setCellFactory(cellFactoryForMap);
//        secondDataColumn.setCellFactory(cellFactoryForMap);
        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table_view);
        ((Group) scene.getRoot()).getChildren().addAll(vbox);
        stage.setScene(scene);
        stage.show();
    }

    private ObservableList<Map> generateDataInMap() {
        int max = 10;
        ObservableList<Map> allData = FXCollections.observableArrayList();
        for (int i = 1; i < max; i++) {
            Map<String, String> dataRow = new HashMap<>();
            String value1 = "A" + i;
            String value2 = "B" + i;
            dataRow.put(Column1MapKey, value1);
            dataRow.put(Column2MapKey, value2);
            allData.add(dataRow);
        }
        return allData;
    }
}

just try to change and use it in your way it can also be used directly in resultset

只需尝试以自己的方式更改和使用它也可以直接在结果集中使用

Happy Coding, Happy Innovation

快乐编码,快乐创新

回答by scottb

This doesn't work with PropertyValueFactory because you have not declared your JavaFX beans with the expected naming conventions for the properties you have defined in your data model.

这不适用于 PropertyValueFactory,因为您没有使用您在数据模型中定义的属性的预期命名约定来声明 JavaFX bean。

Refer to this post for how to use PropertyValueFactory correctly: How to use the PropertyValueFactory correctly?

有关如何正确使用 PropertyValueFactory,请参阅此帖子:如何正确 使用 PropertyValueFactory?