JavaFX com SceneBuilder

7 respostas
java
W

Olá, estou tentando a dias resolver um problema na minha aplicação:

alguém sabe me dizer porque o JavaFX não retorna o campo ID na TableView?

Segue abaixoo code do Controller:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import model.Contato;

/**
 * FXML Controller class
 *
 * @author wssco
 */
public class FXMLContatoGuiController implements Initializable {

    private final ObservableList<Contato> dadosContato;
    private final ContatoController contatoController;

    public FXMLContatoGuiController() {
        contatoController = new ContatoController();
        dadosContato = FXCollections.observableArrayList(contatoController.lista());
    }

    @FXML
    private TableView contatoTable;

    @FXML
    private TableColumn idCol;
    @FXML
    private TableColumn nomeCol;
    @FXML
    private TableColumn foneCol;
    @FXML
    private TableColumn emailCol;

    @FXML
    private TextField nomeTextField;
    @FXML
    private TextField foneTextField;
    @FXML
    private TextField emailTextField;

    @FXML
    private Button gravarButton;

    /**
     * Initializes the controller class.
     *
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        preencherTabela();
    }

    @FXML
    public void eventGravarButtonClick(ActionEvent event) {
        Contato c = new Contato();
        try {
            c.setNome(nomeTextField.getText());
            c.setFone(foneTextField.getText());
            c.setEmail(emailTextField.getText());
            contatoController.gravar(c);
            alert("Gravado com sucesso");
        } catch (Exception ex) {
            alert(ex.getMessage());
        }
    }

    public void preencherTabela() {
        idCol.setCellValueFactory(new PropertyValueFactory("ID"));
        nomeCol.setCellValueFactory(new PropertyValueFactory("Nome"));
        foneCol.setCellValueFactory(new PropertyValueFactory("Fone"));
        emailCol.setCellValueFactory(new PropertyValueFactory("Email"));
        contatoTable.setItems(dadosContato);
    }

    public void alert(String msg) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText(null);
        alert.setTitle("Mensagem");
        alert.setContentText(msg);
        alert.show();
    }

}

7 Respostas

W

Segue o modelo

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 *
 * @author wssco
 */
@Entity(name = "contato")
public class Contato implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column(length = 50, nullable = false)
    private String nome;
    @Column(length = 20, nullable = false)
    private String fone;
    @Column(length = 250)
    private String email;

    public Contato() {
    }

    public Contato(int id, String nome, String fone, String email) {
        this.id = id;
        this.nome = nome;
        this.fone = fone;
        this.email = email;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the nome
     */
    public String getNome() {
        return nome;
    }

    /**
     * @param nome the nome to set
     */
    public void setNome(String nome) {
        this.nome = nome;
    }

    /**
     * @return the fone
     */
    public String getFone() {
        return fone;
    }

    /**
     * @param fone the fone to set
     */
    public void setFone(String fone) {
        this.fone = fone;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

}
S

Faz um sysout nessa observableList e veja se o id está nela.

A

Olá, creio que o erro esteja em colocar o “ID” em caixa alta sendo que o get do model está só o “i” em caixa alta. No caso seria new PropertyValueFactory(“Id”), ou seja ele é case sensitive.
Veja esse link How to use the PropertyValueFactory correctly?

J

Utilizando a lambda vc pode dizer qual campo do Objeto que vai popular sua coluna, mas para isso a

TableColumn<Contato, Integer>

Assim bastava fazer isso aqui:

idCol.setCellValueFactory(cellData -> cellData.getValue().getId());
A

Verdade JeanJavaMan! atualmente faço assim. Outro link para sobre: Showing Object Properties in a TableView

W

Valeu @Andrauss mudei a caixa alta e funcionou.

public void preencherTabela() {
        idCol.setCellValueFactory(new PropertyValueFactory("Id"));
        nomeCol.setCellValueFactory(new PropertyValueFactory("Nome"));
        foneCol.setCellValueFactory(new PropertyValueFactory("Fone"));
        emailCol.setCellValueFactory(new PropertyValueFactory("Email"));
        contatoTable.setItems(dadosContato);
    }
J

Pois é, eu gostei mais dessa maneira :slight_smile:

Criado 26 de janeiro de 2016
Ultima resposta 26 de jan. de 2016
Respostas 7
Participantes 4