0
Good morning!
When I run the code my Tableview pulls 2 blank lines. I am not able to show the inforamations in table view. Follow code below:
Controller:
package br.com.unitymanager.controller;
import br.com.unityamanager.tableview.TableCliente;
import br.com.unitymanager.dao.ClienteDao;
import br.com.unitymanager.model.Cliente;
import br.com.unitymanager.stage.Telas;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
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.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class FXMLTelaGerenciadorEmpresaController implements Initializable {
@FXML private TableView<TableCliente> tbCliente;
@FXML private TableColumn<TableCliente, Integer> colunaCodigoCliente;
@FXML private TableColumn<TableCliente, String> colunaDoc;
@FXML private TableColumn<TableCliente, String> colunaRazao;
@FXML private TableColumn<TableCliente, String> colunaTelefone;
@FXML private Button btnCadastrar;
private final Telas chamarTelas = new Telas();
private ClienteDao daoCliente = new ClienteDao();
private List<Cliente> listCliente = daoCliente.listarClientes();
private ObservableList<TableCliente> listPessoaTabela = FXCollections.observableArrayList();
private void populandoTabela(){
if(!listPessoaTabela.isEmpty()){
listPessoaTabela.clear();
}
for(Cliente cliente : listCliente){
TableCliente tabCli = new TableCliente(cliente);
tabCli.mostrandoInforamcoes();
listPessoaTabela.add(tabCli);
}
colunaCodigoCliente.setCellValueFactory(new PropertyValueFactory<TableCliente, Integer>("Cód.Cliente"));
colunaDoc.setCellValueFactory(new PropertyValueFactory<TableCliente, String>("CNPJ/CPF"));
colunaRazao.setCellValueFactory(new PropertyValueFactory<TableCliente, String>("Razão Social"));
colunaTelefone.setCellValueFactory(new PropertyValueFactory<TableCliente, String>("Telefone"));
tbCliente.setItems(listPessoaTabela);
}
@FXML
void actionButtonCadastrarCliente(ActionEvent event) throws IOException {
chamarTelas.telaCadastroCliente();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
populandoTabela();
}
}
Tablecliente:
package br.com.unityamanager.tableview;
import br.com.unitymanager.model.Cliente;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class TableCliente {
private final SimpleIntegerProperty codCliente;
private final SimpleStringProperty docCliente;
private final SimpleStringProperty razaoSocialCliente;
private final SimpleStringProperty telefoneCliente;
private Cliente client;
public TableCliente(Cliente cli){
this.client = cli;
this.codCliente = new SimpleIntegerProperty(cli.getCodigoCliente());
this.docCliente = new SimpleStringProperty(cli.getDocCliente());
this.razaoSocialCliente = new SimpleStringProperty(cli.getRazaoSocialCliente());
this.telefoneCliente = new SimpleStringProperty(cli.getTelefoneCliente());
}
public Cliente getCliente() {
return client;
}
public void mostrandoInforamcoes(){
System.out.println(this.codCliente);
System.out.println(this.docCliente);
System.out.println(this.razaoSocialCliente);
System.out.println(this.telefoneCliente);
}
public int getCodCliente() {
return codCliente.get();
}
public String getDocCliente() {
return docCliente.get();
}
public String getRazaoSocialCliente() {
return razaoSocialCliente.get();
}
public String getTelefoneCliente() {
return telefoneCliente.get();
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="656.0" prefWidth="589.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="br.com.unitymanager.controller.FXMLTelaGerenciadorEmpresaController">
<stylesheets>
<URL value="@/styles/TelaGerenciadorEmpresa.css" />
</stylesheets>
<children>
<TitledPane animated="false" collapsible="false" layoutX="13.0" layoutY="14.0" prefHeight="139.0" prefWidth="558.0" stylesheets="@../styles/TelaGerenciadorEmpresa.css" text="Filtro de Pesquisa" textFill="WHITE">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Cód. Cliente" />
<TextField layoutX="87.0" layoutY="10.0" prefHeight="25.0" prefWidth="113.0" />
<Label layoutX="12.0" layoutY="57.0" text="Razão Social:" />
<TextField layoutX="87.0" layoutY="53.0" prefHeight="25.0" prefWidth="372.0" />
<Label layoutX="224.0" layoutY="14.0" text="CNPJ/CPF:" />
<TextField layoutX="289.0" layoutY="10.0" prefHeight="25.0" prefWidth="255.0" />
<Button layoutX="474.0" layoutY="53.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="70.0">
<graphic>
<FontAwesomeIconView fill="ROYALBLUE" glyphName="SEARCH" size="16" />
</graphic>
</Button>
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" collapsible="false" layoutX="14.0" layoutY="164.0" prefHeight="438.0" prefWidth="558.0" stylesheets="@../styles/TelaGerenciadorEmpresa.css" text="Resultado da Pesquisa" textFill="WHITE">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="412.0" prefWidth="548.0" stylesheets="@../styles/TelaGerenciadorEmpresa.css">
<children>
<TableView fx:id="tbCliente" prefHeight="414.0" prefWidth="558.0">
<columns>
<TableColumn fx:id="colunaCodigoCliente" prefWidth="75.0" text="Cód. Cliente" />
<TableColumn fx:id="colunaDoc" prefWidth="115.0" text="CNPJ/CPF" />
<TableColumn fx:id="colunaRazao" prefWidth="250.0" text="Razão Social" />
<TableColumn fx:id="colunaTelefone" minWidth="0.0" prefWidth="116.0" text="Telefone" />
</columns>
</TableView>
</children>
</AnchorPane>
</content>
</TitledPane>
<Button fx:id="btnCadastrar" layoutX="13.0" layoutY="617.0" mnemonicParsing="false" onAction="#actionButtonCadastrarCliente" prefHeight="25.0" prefWidth="106.0" text="Cadastrar">
<graphic>
<FontAwesomeIconView fill="#08f800" glyphName="PLUS" size="16" />
</graphic>
</Button>
</children>
</AnchorPane>
Clientele:
package br.com.unitymanager.dao;
import br.com.unitymanager.model.Cliente;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class ClienteDao {
private final EntityManagerFactory emf = Persistence.createEntityManagerFactory("BancoPU");
private final EntityManager em = emf.createEntityManager();
public void adicionarUsuario(Cliente cliente){
try {
em.getTransaction().begin();
em.persist(cliente);
em.getTransaction().commit();
} catch (Exception ex) {
ex.printStackTrace();
em.getTransaction().rollback();
}
}
public List<Cliente> listarClientes(){
List<Cliente> resultado = new ArrayList<>();
em.getTransaction().begin();
resultado = em.createQuery("from Cliente").getResultList();
em.getTransaction().commit();
em.close();
return resultado;
}
}