Estou praticando JavaFX em uma criação de um mini projeto, porém estou com problemas para adicionar dados na tableview a partir de classe de produtos, utilizando uma lista. Basicamente eu quero adicionar dados através de um textfield para Lista do tipo Produtos e depois adicionar a minha TableView.
Já tente fazer vários outros tipos de abordagem simples mais simples não aparece nenhum dado na minha tabela!
Link do projeto completo: https://github.com/Wesleyhrl/Gerenciador_Estoque
Classe Main Controller:
package visao;
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.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyEvent;
import modelo.ListaProduto;
import modelo.Produto;
public class MainController implements Initializable {
@FXML
private Button btnBuscar;
@FXML
private Button btnImpExp;
@FXML
private Button btnInicio;
@FXML
private Button btnNovoProduto;
@FXML
private Button btnRelatorio;
@FXML
private Button btnSair;
@FXML
private static TableView<Produto> tab = new TableView<>();
@FXML
private TableColumn<Produto, String> colCodigo = new TableColumn<>();
@FXML
private TableColumn<Produto, String> colData = new TableColumn<>();
@FXML
private TableColumn<Produto, String> colDescricao = new TableColumn<>();
@FXML
private TableColumn<Produto, String> colGrupo = new TableColumn<>();
@FXML
private TableColumn<Produto, String> colNome = new TableColumn<>();
@FXML
private TableColumn<Produto, Integer> colQtde = new TableColumn<>();
@FXML
private TableColumn<Produto, Double> colValor = new TableColumn<>();
@FXML
private TextField txtBusca;
@FXML
void actionBusca(ActionEvent event) {
}
@FXML
void actionEditar(ActionEvent event) {
App.cenaEditProduto();
}
@FXML
void actionImpExp(ActionEvent event) {
}
@FXML
void actionInicio(ActionEvent event) {
}
@FXML
void actionNovo(ActionEvent event) {
App.cenaNewProduto();
}
@FXML
void actionRelatorio(ActionEvent event) {
}
@FXML
void actionSair(ActionEvent event) {
}
@FXML
void keyBuscar(KeyEvent event) {
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
System.out.println("Inicio");
colNome.setCellValueFactory(new PropertyValueFactory<Produto, String>("nome"));
colCodigo.setCellValueFactory(new PropertyValueFactory<Produto, String>("codigo"));
colQtde.setCellValueFactory(new PropertyValueFactory<Produto, Integer>("quantidade"));
colGrupo.setCellValueFactory(new PropertyValueFactory<Produto, String>("grupo"));
colValor.setCellValueFactory(new PropertyValueFactory<Produto, Double>("valor"));
colDescricao.setCellValueFactory(new PropertyValueFactory<Produto, String>("descricao"));
colData.setCellValueFactory(new PropertyValueFactory<Produto, String>("data"));
}
static void preencherTab(List<Produto> ListaProduto) {
ObservableList<Produto> obsListprodutos = FXCollections.observableArrayList(ListaProduto);
tab.setItems(obsListprodutos);
System.out.println("Tabela");
}
}
Classe NewProdutoController:
package visao;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import modelo.ListaProduto;
import modelo.Produto;
public class NewProdutoController{
@FXML
private Button btnSalvar;
@FXML
private TextField txtCodigo;
@FXML
private TextField txtData;
@FXML
private TextField txtDescricao;
@FXML
private TextField txtGrupo;
@FXML
private TextField txtNome;
@FXML
private TextField txtQtde;
@FXML
private TextField txtValor;
ListaProduto produtos = new ListaProduto();
@FXML
void actionNewSalvar(ActionEvent event) {
Produto p = new Produto(txtNome.getText(), txtCodigo.getText(), Integer.parseInt(txtQtde.getText()),
txtGrupo.getText(), Double.parseDouble(txtValor.getText()), txtDescricao.getText(), txtData.getText());
System.out.println(p.getNome());
for (Produto elemento : produtos.getProdutos()) {
System.out.println(elemento.getNome());
}
produtos.gravar(p);
MainController.preencherTab(produtos.getProdutos());
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2015, 2019, Gluon and/or its affiliates.
All rights reserved. Use is subject to license terms.
This file is available and licensed under the following license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
- Neither the name of Oracle Corporation nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<?import javafx.geometry.Insets?>
<?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.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="580.0" prefWidth="967.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="visao.MainController">
<children>
<Pane prefHeight="82.0" prefWidth="1002.0" style="-fx-background-color: #00c6ff;">
<children>
<Button fx:id="btnBuscar" layoutX="768.0" layoutY="26.0" mnemonicParsing="false" onAction="#actionBusca" prefHeight="28.0" prefWidth="63.0" style="-fx-background-color: #F0F0F0; -fx-border-style: solid; -fx-border-color: #F0F0F0; -fx-border-width: 4px;" text="Buscar">
<font>
<Font name="Lucida Bright" size="11.8" />
</font>
</Button>
<TextField fx:id="txtBusca" layoutX="831.0" layoutY="26.0" onKeyPressed="#keyBuscar" prefHeight="31.0" prefWidth="160.0" style="-fx-background-color: white; -fx-border-style: solid; -fx-border-color: white; -fx-border-width: 3px;" />
<ImageView fitHeight="62.0" fitWidth="83.0" layoutX="8.0" layoutY="10.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../iconWhite.png" />
</image>
</ImageView>
<Label layoutX="78.0" layoutY="24.0" text="Easy Stock" textFill="WHITE">
<font>
<Font name="Lucida Bright Demibold" size="24.0" />
</font>
</Label>
</children>
</Pane>
<VBox layoutY="82.0" prefHeight="498.0" prefWidth="217.0" spacing="10.0" style="-fx-background-color: #F0F0F0;">
<children>
<Button fx:id="btnInicio" alignment="CENTER" mnemonicParsing="false" onAction="#actionInicio" prefHeight="0.0" prefWidth="209.0" style="-fx-background-color: #00c6ff; -fx-border-style: solid; -fx-border-color: #00c6ff; -fx-border-width: 4px;" text="Inicio" textFill="WHITE">
<font>
<Font name="Lucida Bright Demibold Italic" size="18.0" />
</font>
<opaqueInsets>
<Insets />
</opaqueInsets>
</Button>
<Button fx:id="btnNovoProduto" alignment="CENTER" mnemonicParsing="false" onAction="#actionNovo" prefHeight="30.0" prefWidth="209.0" style="-fx-background-color: #00c6ff; -fx-border-style: solid; -fx-border-color: #00c6ff; -fx-border-width: 4px;" text="Novo Produto" textFill="WHITE">
<font>
<Font name="Lucida Bright Demibold Italic" size="18.0" />
</font>
<opaqueInsets>
<Insets />
</opaqueInsets>
<VBox.margin>
<Insets />
</VBox.margin>
</Button>
<Button fx:id="btnNovoProduto" alignment="CENTER" layoutX="15.0" layoutY="73.0" mnemonicParsing="false" onAction="#actionEditar" prefHeight="30.0" prefWidth="209.0" style="-fx-background-color: #00c6ff; -fx-border-style: solid; -fx-border-color: #00c6ff; -fx-border-width: 4px;" text="Editar Produto" textFill="WHITE">
<font>
<Font name="Lucida Bright Demibold Italic" size="18.0" />
</font>
<opaqueInsets>
<Insets />
</opaqueInsets>
</Button>
<Button fx:id="btnRelatorio" alignment="CENTER" mnemonicParsing="false" onAction="#actionRelatorio" prefHeight="0.0" prefWidth="209.0" style="-fx-background-color: #00c6ff; -fx-border-style: solid; -fx-border-color: #00c6ff; -fx-border-width: 4px;" text="Relatório" textFill="WHITE">
<font>
<Font name="Lucida Bright Demibold Italic" size="18.0" />
</font>
<opaqueInsets>
<Insets />
</opaqueInsets>
</Button>
<Button fx:id="btnImpExp" alignment="CENTER" layoutX="15.0" layoutY="126.0" mnemonicParsing="false" onAction="#actionImpExp" prefHeight="0.0" prefWidth="209.0" style="-fx-background-color: #00c6ff; -fx-border-style: solid; -fx-border-color: #00c6ff; -fx-border-width: 4px;" text="Imp/Exp" textFill="WHITE">
<font>
<Font name="Lucida Bright Demibold Italic" size="18.0" />
</font>
<opaqueInsets>
<Insets />
</opaqueInsets>
</Button>
<Pane prefHeight="218.0" prefWidth="207.0" />
<Button fx:id="btnSair" alignment="CENTER" layoutX="15.0" layoutY="179.0" mnemonicParsing="false" onAction="#actionSair" prefHeight="0.0" prefWidth="209.0" style="-fx-background-color: #00c6ff; -fx-border-style: solid; -fx-border-color: #00c6ff; -fx-border-width: 4px;" text="Sair" textFill="WHITE">
<font>
<Font name="Lucida Bright Demibold Italic" size="18.0" />
</font>
<opaqueInsets>
<Insets />
</opaqueInsets>
</Button>
</children>
<padding>
<Insets bottom="10.0" left="5.0" right="5.0" top="10.0" />
</padding>
<opaqueInsets>
<Insets />
</opaqueInsets>
</VBox>
<GridPane alignment="TOP_CENTER" layoutX="217.0" layoutY="82.0" prefHeight="498.0" prefWidth="785.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="244.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="481.0" minHeight="10.0" prefHeight="458.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TableView fx:id="tab" prefHeight="366.0" prefWidth="785.0" GridPane.rowIndex="1">
<columns>
<TableColumn fx:id="colNome" prefWidth="93.0" text="Nome" />
<TableColumn fx:id="colCodigo" prefWidth="86.0" text="Código" />
<TableColumn fx:id="colQtde" prefWidth="73.0" text="Quantidade" />
<TableColumn fx:id="colGrupo" minWidth="0.0" prefWidth="90.0" text="Grupo" />
<TableColumn fx:id="colValor" prefWidth="70.0" text="Valor" />
<TableColumn fx:id="colDescricao" prefWidth="278.0" text="Descrição" />
<TableColumn fx:id="colData" prefWidth="86.0" text="Data" />
</columns>
<GridPane.margin>
<Insets bottom="4.0" left="4.0" right="4.0" />
</GridPane.margin>
</TableView>
<Pane prefHeight="200.0" prefWidth="200.0">
<children>
<Label alignment="CENTER" layoutX="344.0" layoutY="6.0" text="Produtos">
<font>
<Font name="Lucida Bright Demibold" size="24.0" />
</font>
</Label>
</children>
</Pane>
</children>
</GridPane>
</children>
</AnchorPane>