0

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

IntegerProperty [值:72] 等...

我怎样才能只显示值填充我的行?

我的表视图代码:

@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 类:

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

}
4

3 回答 3

1

错误出现在我的 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);
    }

}
于 2013-01-28T17:30:39.847 回答
0

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

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

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

快乐编码,快乐创新

于 2013-05-11T18:13:30.403 回答
0

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

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

于 2013-05-11T19:13:53.930 回答