Victor Laerte
Victor Laerte

Reputation: 6556

Inserting data in a TableView Javafx

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

IntegerProperty [value: 72] and etc...

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

My TableView code:

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

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

}

Upvotes: 0

Views: 18318

Answers (3)

scottb
scottb

Reputation: 10084

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.

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

Upvotes: 0

Ranjeet Rana
Ranjeet Rana

Reputation: 65

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

Upvotes: 0

Victor Laerte
Victor Laerte

Reputation: 6556

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

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

}

Upvotes: 1

Related Questions