0

I have a TableView that I want to populate with data from some TextFields, to do this, I copied code from one other TableView I have in my aplication since it was working. But in this second TableView, the data is not being shown as rows in the table. I know the data was added because I can switch between the rows using the arrow keys on the keyboard, and in the debugger mode, the items property is updated every time I add the rows. In the other TableView the code is working properly but not on the second, any ideas on how to fix this? I read the other questions talking about the PropertyValueFactory, but the thing is, the first example works and the second one doesn't. I tried changing the name of the variables but still had no look.

Class where the data is stored:

public class Produto {
    private String nome, codigo;
    private double preco, precoCompra;
    private int quantidade;

    public String getNome(){return nome;}
    public String getCodigo(){return codigo;}
    public double getPreco(){return preco;}
    public int getQuantidade(){return quantidade;}
    public double getPrecoCompra(){return precoCompra;}

    public Produto (int quantidade, String nome) {
        this.quantidade = quantidade;
        this.nome = nome;
    }
    public Produto(String nome, String codigo, double preco, double precoCompra) {
        this.nome = nome;
        this.codigo = codigo;
        this.preco = preco;
        this.precoCompra = precoCompra;
    }
    public Produto(String nome, String codigo, double preco, double precoCompra, int quantidade) {
        this.nome = nome;
        this.codigo = codigo;
        this.preco = preco;
        this.precoCompra = precoCompra;
        this.quantidade = quantidade;
    }

    public Produto(String nome, int quantidade){
        this.nome = nome;
        this.quantidade = quantidade;
    }
}

Working code from TableView 1:

Controller:

public class VerEstoque {
    @FXML
    BorderPane root;
    @FXML
    StackPane rootVE;

    @FXML
    private TableView tabela = new TableView();
    private TableColumn<Produto, String> codigo = new TableColumn<>("CODIGO");
    private TableColumn<Produto, String> nome = new TableColumn<>("NOME");
    private TableColumn<Produto, Double> preco = new TableColumn<>("PREÇO DE VENDA");
    private TableColumn<Produto, Double> precoCompra = new TableColumn<>("PREÇO DE COMPRA");
    private TableColumn<Produto, Integer> quantidade = new TableColumn<>("QUANTIDADE");

    private static ArrayList<Produto> produtos = new ArrayList<>();
    private static ObservableList<Produto> listaProdutos = FXCollections.observableArrayList();

    @FXML
    private void initialize() {
        Main.roots.add(rootVE);
        tabela.getColumns().addAll(codigo, nome, preco, precoCompra,  quantidade);
        codigo.setCellValueFactory(new PropertyValueFactory<>("codigo"));
        nome.setCellValueFactory(new PropertyValueFactory<>("nome"));
        preco.setCellValueFactory(new PropertyValueFactory<>("preco"));
        precoCompra.setCellValueFactory(new PropertyValueFactory<>("precoCompra"));
        quantidade.setCellValueFactory(new PropertyValueFactory<>("quantidade"));
        tabela.setItems(listaProdutos);
        tabela.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        buscarEstoque();
    }

    static void buscarEstoque() {
        //In this case the data is being pulled from a database and added to an arraylist
        BancoDeDados bd = BancoDeDados.getInstance();
        try {
            ResultSet rs = bd.select("SELECT * FROM Estoque");
            while (rs.next()) {
                produtos.add(new Produto(rs.getString("Nome"), rs.getString("Codigo"), rs.getDouble("PrecoVenda"), rs.getDouble("PrecoCompra") , rs.getInt("Quantidade")));
            }
            rs.close();
            listaProdutos.clear();
            listaProdutos.addAll(produtos);
            produtos.clear();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

FXML File:

<StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:id="rootVE"
       prefHeight="400.0" prefWidth="600.0" fx:controller="mercado.controllers.VerEstoque">
<BorderPane prefHeight="400.0" prefWidth="600.0" fx:id="root">
    <top>
        <HBox alignment="CENTER">
            <children>
                <Label text="ESTOQUE" fx:id="titulo"/>
            </children>
        </HBox>
    </top>
    <center>
        <TableView fx:id="tabela"/>
    </center>
</BorderPane>

The above code works as intended but I have errors in the code below:

Controller:

public class DefinirProdutos {

    @FXML private StackPane rootDP;
    @FXML private VBox base;
    @FXML private TextField campoQtd, campoNome;
    @FXML private TableView tabela;
    private TableColumn<Produto, Integer> qtd = new TableColumn<>("Qtd");
    private TableColumn<Produto, String> nome = new TableColumn<>("Produto");
    private  ArrayList<Produto> arrayProdutos = new ArrayList<>();
    private  ObservableList<Produto> listaProdutos = FXCollections.observableArrayList();

    @FXML
    private void initialize() {
        tabela.getColumns().addAll(qtd, nome);
        tabela.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        campoNome.setPromptText("Produto");
        campoQtd.setPromptText("Qtd");
        nome.setCellValueFactory(new PropertyValueFactory<>("produto"));
        qtd.setCellValueFactory(new PropertyValueFactory<>("qtd"));
        tabela.setItems(listaProdutos);
     }

    @FXML
    private void adicionarProduto() {
        int valorQtd = campoQtd.getText().length();
        int valorNome = campoNome.getText().length();
        if(valorQtd == 0 && valorNome == 0){
            //warn that the textfields are empty, irrelevant
        }else if(valorQtd == 0){
            //warn that the textfield is empty
        }else if(valorNome == 0){
            //warn that the textfield is empty
        }else{
            //Codigo pra caso nao estiver adicionado na array
            arrayProdutos.add(new Produto(Integer.parseInt(campoQtd.getText()), campoNome.getText()));
            listaProdutos.clear();
            listaProdutos.addAll(arrayProdutos);
            tabela.refresh();
        }
    }
}

FXML for the tableview that does not show:

<StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" prefHeight="600.0" 
maxWidth="800.0" fx:id="rootDP"
fx:controller="mercado.controllers.DefinirProdutos">
    <VBox fx:id="base" spacing="30">
        <children>
            <HBox alignment="CENTER">
                <children>
                    <Label text="DEFINIR PRODUTOS" fx:id="titulo"/>
                </children>
            </HBox>
            <HBox alignment="CENTER" spacing="10">
                <children>
                    <TextField fx:id="campoQtd" alignment="CENTER"/>
                    <TextField alignment="CENTER" fx:id="campoNome"/>
                    <Button text="Adicionar" prefWidth="140" prefHeight="40" fx:id="btnAdd" 
onAction="#adicionarProduto"/>
                </children>
            </HBox>
            <TableView fx:id="tabela"/>
            <HBox alignment="CENTER" fx:id="confirmar">
                <children>
                    <Button text="Feito" prefWidth="200" prefHeight="40" fx:id="btnFeito"/>
                </children>
            </HBox>
        </children>
     </VBox>
 </StackPane>
JoeyJohnJo
  • 82
  • 2
  • 8
  • 1
    If you are using the same data object (Producto) as you specified, i think nome.setCellValueFactory(new PropertyValueFactory<>("produto")); qtd.setCellValueFactory(new PropertyValueFactory<>("qtd")); will not work. As "produto" & "qtd" are not properties in Producto. – Sai Dandem Oct 10 '19 at 00:25
  • Jeez, thanks for pointing that out, now it works haha. "produto" and "qtd" are properties I completly forgot to rename, from some previous tests I was making. Thank you very much :) – JoeyJohnJo Oct 10 '19 at 00:32

2 Answers2

1

Reflection is usually the worst possible way to accomplish a task, and this is one of the reasons.

When you use reflection, the compiler cannot check whether you’ve used an incorrect or misspelled name for a class, method, field, or bean property. In this case, the compiler cannot check whether the argument you pass to a PropertyValueFactory constructor is valid.

A better approach is to avoid using PropertyValueFactory entirely, and instead write your own cell value factory for each TableColumn:

codigo.setCellValueFactory(
    f -> new SimpleStringProperty(f.getValue().getCodigo()));
nome.setCellValueFactory(
    f -> new SimpleStringProperty(f.getValue().getNome()));
preco.setCellValueFactory(
    f -> new SimpleDoubleProperty(f.getValue().getPreco()));
precoCompra.setCellValueFactory(
    f -> new SimpleDoubleProperty(f.getValue().getPrecoCompra()));
quantidade.setCellValueFactory(
    f -> new SimpleIntegerProperty(f.getValue().getQuantidade()));

If we make a similar change to your TableColumns in the DefinirProdutos class:

nome.setCellValueFactory(
    f -> new SimpleStringProperty(f.getValue().getProduto()));
qtd.setCellValueFactory(
    f -> new SimpleIntegerProperty(f.getValue().getQtd()));

the compiler will helpfully inform you that those methods do not exist.

VGR
  • 40,506
  • 4
  • 48
  • 63
0

As pointed out by Sai Dandem, my property names for the columns in setCellValueFactory(new PropertyValueFactory<>() were wrong. The issue was fixed by renaming them as the properties in the data object

JoeyJohnJo
  • 82
  • 2
  • 8