how to show data in tableview with button

0

I want to show all the records that I save in the database with a tableview, the code of the insertion and everything else is fine, but as I can call the initialize method through the button, that is to say that when I click show all the records of the bd, the records are shown but not using the button.

this is my code.

package application;

import javafx.collections.*;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;

public class Mostraregistros  implements Initializable  {

     ObservableList <cliente> data =FXCollections.observableArrayList();
    @FXML TableView<cliente>  tablacliente;
    @FXML TableColumn<cliente, String> nombrescol;
    @FXML TableColumn<cliente,String > apellidoscol;
    @FXML TableColumn<cliente, Integer>  clienteid;
    @FXML private Button mtn;


    @FXML

     private void  mtn(ActionEvent event) { 

         mtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {

                    Alert alert = new Alert(AlertType.INFORMATION);
                    alert.setTitle("Informacion");
                    alert.setHeaderText(null);
                    alert.setContentText("Mostrando Todos los Registros");
                 alert.showAndWait();


                }
            });



     }
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {

  llamar boton aca.
         Connection conn=null;{
         try {

             conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
             Statement mostrar=conn.createStatement();
             ResultSet rs;
             rs= mostrar.executeQuery("select * from cliente");


             while ( rs.next() ) 
             {
                data.add(new cliente(
                        rs.getString("nombre"),
                        rs.getString("apellido"),
                        rs.getInt("id")
                        ));

             }

             nombrescol.setCellValueFactory(new PropertyValueFactory <cliente, String>("nombres"));
            apellidoscol.setCellValueFactory(new PropertyValueFactory <cliente, String>("apellidos"));
            clienteid.setCellValueFactory(new PropertyValueFactory <cliente, Integer>("id_cliente"));
            tablacliente.setItems(data);

             if(conn!=null)
                 System.out.println("conexion exitosa");


         } catch (SQLException e) {
             e.printStackTrace();
         }
    }

    }

}

improve the code, but it does not show me the last record I attach, when I run the software it shows them all, but when I add a new one and click on the button show does not show that new record, I have to close the program, execute it again and there is if it shows the last record, I post the correction of the previous code so that they help me porfa

package application;

import javafx.collections.*;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;


import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;

public class Mostraregistros  implements Initializable  {

     ObservableList <cliente> data =FXCollections.observableArrayList();
    @FXML TableView<cliente>  tablacliente;
    @FXML TableColumn<cliente, String> nombrescol;
    @FXML TableColumn<cliente,String > apellidoscol;
    @FXML TableColumn<cliente, Integer>  clienteid;
    @FXML private Button mtn;





    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {

         mtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {

                    Alert alert = new Alert(AlertType.INFORMATION);
                    alert.setTitle("Informacion");
                    alert.setHeaderText(null);
                    alert.setContentText("Mostrando Todos los Registros");
              alert.showAndWait();
              nombrescol.setCellValueFactory(new PropertyValueFactory <cliente, String>("nombres"));
                apellidoscol.setCellValueFactory(new PropertyValueFactory <cliente, String>("apellidos"));
                clienteid.setCellValueFactory(new PropertyValueFactory <cliente, Integer>("id_cliente"));
                tablacliente.setItems(data);

                }
            });   
         Connection conn=null;{
         try {

             conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
             Statement mostrar=conn.createStatement();
             ResultSet rs;
             rs= mostrar.executeQuery("select * from cliente");


             while ( rs.next() ) 
             {
                data.add(new cliente(
                        rs.getString("nombre"),
                        rs.getString("apellido"),
                        rs.getInt("id")
                        ));

             }



             if(conn!=null)
                 System.out.println("conexion exitosa");


         } catch (SQLException e) {
             e.printStackTrace();
         }
    }

    }

}

this is the fxml code

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.cell.*?>
<?import application.cliente.*?>
<?import application.Mostraregistros.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="634.0" prefWidth="626.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ConexionSQL">
   <children>
      <Pane layoutX="5.0" layoutY="1.0" prefHeight="581.0" prefWidth="977.0">
         <children>
            <TextField fx:id="nm" layoutX="125.0" layoutY="70.0" prefHeight="32.0" prefWidth="205.0" text="nombres" />
            <TextField fx:id="ap" layoutX="125.0" layoutY="133.0" prefHeight="32.0" prefWidth="205.0" text="apellidos" />
            <Label layoutX="27.0" layoutY="70.0" prefHeight="32.0" prefWidth="137.0" text="NOMBRES" />
            <Button fx:id="btn" layoutX="21.0" layoutY="216.0" mnemonicParsing="false" onAction="#btn" prefHeight="43.0" prefWidth="84.0" text="AGREGAR" />
            <Label layoutX="27.0" layoutY="141.0" text="APELLIDOS" />
            <TableView fx:id="tablacliente" layoutX="346.0" layoutY="47.0" prefHeight="448.0" prefWidth="553.0">
              <columns> 
                  <TableColumn fx:id="clienteid" prefWidth="225.0" text="CLIENTE ID"/> 

                <TableColumn fx:id="nombrescol" prefWidth="206.0" text="NOMBRES"/>

                <TableColumn fx:id="apellidoscol" prefWidth="161.0" text="APELLIDOS"/>

              </columns>
            </TableView>
            <Button fx:id="mtn" layoutX="138.0" layoutY="216.0" mnemonicParsing="false" prefHeight="43.0" prefWidth="144.0" text="MOSTRAR REGISTROS" />
         </children>
      </Pane>
   </children>
</AnchorPane>
    
asked by Rafael 10.10.2018 в 04:58
source

0 answers