How and where to put the onAction of a button inside a tableView?

0

I have a button inside a tableview, this button should allow me to remove the information from the list.

The information comes from a database, that is, it is not "burned" information, the problem I have is that I do not know where in my code I should put the onAction, since this was putting it in the document FXML, but being a button inside a table, I can not do it like that. To program I used a multilayer pattern, defining the classes and their attributes in DTO , the relationship with the database in DAO and as I said before, FXML and its respective controller.

DTO

public class Persona {
    private Long identificacion;
    private String nombre;
    private String apellido;
    private Integer idTipoPersona;
    private String contrasena;
    private String email;
    private String direccion;
    private Button boton;

    public Long getIdentificacion() {
        return identificacion;
    }

    public void setIdentificacion(Long identificacion) {
        this.identificacion = identificacion;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getApellido() {
        return apellido;
    }

    public void setApellido(String apellido) {
        this.apellido = apellido;
    }

    public Integer getIdTipoPersona() {
        return idTipoPersona;
    }

    public void setIdTipoPersona(Integer idTipoPersona) {
        this.idTipoPersona = idTipoPersona;
    }

    public String getContrasena() {
        return contrasena;
    }

    public void setContrasena(String contrasena) {
        this.contrasena = contrasena;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDireccion() {
        return direccion;
    }

    public void setDireccion(String direccion) {
        this.direccion = direccion;
   }   

    public Button getBoton() {
        return boton;
    }

    public void setBoton(Button boton) {
        this.boton= boton;
    }
}

Here all I did was create the button as an attribute.

DAO

public List<Persona> perAll() throws SQLException {
    List<Persona> listPer = new ArrayList<Persona>();

    Conexion CONEX = new Conexion();
    PreparedStatement ps;
    ResultSet rs;

    String SQLONE = "SELECT * FROM persona WHERE id_tipo_persona = 1 OR id_tipo_persona = 3 OR id_tipo_persona = 4";
    ps = CONEX.getConnection().prepareStatement(SQLONE);
    rs = ps.executeQuery();
    while (rs.next()) {
        persona = new Persona();
        persona.setIdentificacion(rs.getLong("id"));
        persona.setNombre(rs.getString("nombre"));
        persona.setApellido(rs.getString("apellido"));
        persona.setIdTipoPersona(rs.getInt("id_tipo_persona"));
        persona.setContrasena(rs.getString("contraseña"));
        persona.setEmail(rs.getString("email"));
        persona.setDireccion(rs.getString("direccion"));
        Button boton= new Button("Eliminar");
        persona.setBoton(boton);
        listPer.add(persona);
    }return listPer;
}

In DAO I'll just put the list that uses the button. Here I gave the button "value" and added it to the list that is generated from the query.

FXML

<TableView fx:id="listaUsuarios" prefWidth="700">
    <columns>
        <TableColumn styleClass="table" fx:id="id" prefWidth= "80" text= "Id"/>
        <TableColumn styleClass="table" fx:id="nombre" prefWidth= "89" text= "Nombre"/>
        <TableColumn styleClass="table" fx:id="apellido" prefWidth= "80" text= "Apellido"/>
        <TableColumn styleClass="table" fx:id="tipoPer" prefWidth= "80" text= "Tipo Persona"/>
        <TableColumn styleClass="table" fx:id="contra" prefWidth= "80" text= "Contraseña"/>
        <TableColumn styleClass="table" fx:id="email" prefWidth= "80" text= "E-Mail"/>
        <TableColumn styleClass="table" fx:id="dir" prefWidth= "80" text= "Dirección"/>
        <TableColumn styleClass="table" fx:id="boton" prefWidth= "80" text="Acciones"/>
    </columns>
</TableView>

I will only put the tableView that contains this list.

FXMLController

public class UsuarioController implements Initializable {

    PersonaDAO Per;

    @FXML
    private TextField idPer;

    @FXML
    private TableView<Persona> listaUsuarios;

    @FXML
    private TableColumn<Persona, Long> id;

    @FXML
    private TableColumn<Persona, String> nombre;

    @FXML
    private TableColumn<Persona, String> apellido;

    @FXML
    private TableColumn<Persona, Integer> tipoPer;

    @FXML
    private TableColumn<Persona, String> contra;

    @FXML
    private TableColumn<Persona, String> email;

    @FXML
    private TableColumn<Persona, String> dir;

    @FXML
    private TableColumn<Persona, Button> boton;


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

        Per= new PersonaDAO();

        try {
            Collection<Persona> personaCollection = Per.perAll();
            ObservableList<Persona> persona = FXCollections.observableArrayList(personaCollection);
            id.setCellValueFactory(new PropertyValueFactory<>("identificacion"));
            nombre.setCellValueFactory(new PropertyValueFactory<>("nombre"));
            apellido.setCellValueFactory(new PropertyValueFactory<>("apellido"));
            tipoPer.setCellValueFactory(new PropertyValueFactory<>("idTipoPersona"));
            contra.setCellValueFactory(new PropertyValueFactory<>("contrasena"));
            email.setCellValueFactory(new PropertyValueFactory<>("email"));
            dir.setCellValueFactory(new PropertyValueFactory<>("direccion"));
            boton.setCellValueFactory(new PropertyValueFactory<>("boton"));
            listaUsuarios.setItems(persona); 

        } catch (SQLException ex) {
        Logger.getLogger(UsuarioController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

In the controller I declare the DAO method that brings the information and the fields that each one of them will occupy in the table.

    
asked by Santiago Bueno 11.07.2018 в 19:56
source

0 answers