Tableview不显示新添加的数据,仅在重新启动程序(javafx)后显示

时间:2019-03-14 19:04:56

标签: java mysql javafx tableview fxml

重新启动程序时,tableview仅显示新添加的数据。

添加新项目的步骤如下:

  1. dialogCadastro
  2. 确认注册表
  3. 更新表格

但是,更新表不起作用。

这是我的代码:

public class ControllerTelaOperacoes implements Initializable {

    @FXML
    TableView<Item> tblItem;

    @FXML
    TableColumn<Item, String> colCodigoItem;

    @FXML
    TableColumn<Item, String> colDescricaoItem;

    @FXML
    TableColumn<Item, String> colValorItem;

    @FXML
    TableColumn<Item, String> colQuantidadeItem;

    @FXML
    TableColumn<Item, String> colFornecedorItem;

    @FXML
    TextField txtQuantidadeItem;

    @FXML
    TextField txtValorItem;

    @FXML
    TextField txtDescricaoItem;

    // @FXML
    // ComboBox cbxFornecedorItem;

    @FXML
    Button btnConfirmarEstoqueAtualizar;

    @FXML
    Button btnConfirmarEstoqueCadastro;

    @FXML
    TextField txtIDAtualizar;

    @FXML
    Button btnCadastrarItem;

    @FXML
    Button btnAtualizarItem;

    @FXML
    Button btnEfetuarVenda;

    private static Scene dialogEstoqueCadastrar;
    private static Scene dialogEstoqueAtualizar;
    private static Scene dialogEfetuarVenda;
    private String dados;


    public String getDados() {
        return dados;
    }

    public void setDados(String dados) {
        this.dados = dados;
    }

    @FXML
    public void CadastrarItem() throws IOException {
        tblItem.refresh();

        Parent fxmldialogEstoqueCadastro = FXMLLoader
                .load(getClass().getResource("/views/dialogEstoqueCadastrar.fxml"));
        dialogEstoqueCadastrar = new Scene(fxmldialogEstoqueCadastro);

        Stage primaryStage = new Stage();

        Image image = new Image("/img/iconeSistema.png");

        primaryStage.setResizable(false);
        primaryStage.getIcons().add(image);
        primaryStage.setTitle("Cadastrar item");
        primaryStage.setScene(dialogEstoqueCadastrar);
        primaryStage.show();
    }

    @FXML
    public void AtualizarItem() throws IOException {
        tblItem.refresh();
        Parent fxmldialogEstoqueAtualizar = FXMLLoader
                .load(getClass().getResource("/views/dialogEstoqueAtualizar.fxml"));
        dialogEstoqueAtualizar = new Scene(fxmldialogEstoqueAtualizar);

        Stage primaryStage = new Stage();

        Image image = new Image("/img/iconeSistema.png");

        primaryStage.setResizable(false);
        primaryStage.getIcons().add(image);
        primaryStage.setTitle("Atualizar item");
        primaryStage.setScene(dialogEstoqueAtualizar);
        primaryStage.show();
    }

    @FXML
    public void EfetuarVenda() throws IOException {
        tblItem.refresh();

        Parent fxmldialogEfetuarVenda = FXMLLoader.load(getClass().getResource("/views/dialogEfetuarVenda.fxml"));
        dialogEfetuarVenda = new Scene(fxmldialogEfetuarVenda);

        Stage primaryStage = new Stage();

        Image image = new Image("/img/iconeSistema.png");

        primaryStage.setResizable(false);
        primaryStage.getIcons().add(image);
        primaryStage.setTitle("Efetuar Venda");
        primaryStage.setScene(dialogEfetuarVenda);
        primaryStage.show();

    }

    // CONTROLE DE ESTOQUE

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        // conecta tabela com o bd
        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection con;

            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/bdprojetointegrador", "root", "");

            System.out.println("TESTE TABELA 1");

            Statement stmt = con.createStatement();
            System.out.println("TESTE TABELA 2");

            ResultSet rs = stmt.executeQuery("SELECT * FROM item");
            System.out.println("TESTE TABELA 3");

            while (rs.next()) {
                 ObservableList<Item> dados = FXCollections
                        .observableArrayList(new Item(rs.getString("codigo"), rs.getString("descricao"),
                                rs.getString("valor"), rs.getString("quantidade"), rs.getString("fornecedor_codigo")));

                System.out.println("TESTE TABELA 4");

                tblItem.getItems().addAll(dados);
                colCodigoItem.setCellValueFactory(new PropertyValueFactory<>("codigo"));
                colDescricaoItem.setCellValueFactory(new PropertyValueFactory<>("descricao"));
                colValorItem.setCellValueFactory(new PropertyValueFactory<>("valor"));
                colQuantidadeItem.setCellValueFactory(new PropertyValueFactory<>("quantidade"));
                colFornecedorItem.setCellValueFactory(new PropertyValueFactory<>("fornecedor_codigo"));
                System.out.println("TESTE TABELA 5");
            }

            stmt.close();
            rs.close();
            con.close();
            System.out.println("TESTE 6");

        } catch (

        ClassNotFoundException e1) {

            Alert classe = new Alert(AlertType.ERROR);
            classe.setContentText("Classe não encontrada!");
            classe.show();

        } catch (SQLException e2) {
            // ESTÁ CHAMANDO ESTA FUNÇÃO
            Alert classe = new Alert(AlertType.ERROR);
            classe.setContentText("Matricula já existente! ERRO: " + e2);
            classe.show();
        }

    }
}

0 个答案:

没有答案
相关问题