Apply in a JPanel the deleted one of a JButton from a BD SQLite

1

Very Good, It turns out that my program when starting recreates the JButtons based on a DB with a WHILE, and I have already programmed the part in which I can delete the person from a JTable, what happens is that in doing so the JPanel follows showing the JButton that was supposedly already deleted, Any advice?

Method with which I charge the JButtons

public void cargarBotonesInstaladores(){
    try{
        Connection miConexion = DriverManager.getConnection("jdbc:sqlite:instaladores3Claro.db");
    Statement miStatement = miConexion.createStatement();
    ResultSet miResultSet = miStatement.executeQuery("SELECT id, name, identification, color FROM instaladores3Claro");

    while(miResultSet.next()){

            Person p = new Person(miResultSet.getString("name"),
            miResultSet.getString("identification"));

            PersonAction pa = new PersonAction(p);

            JButton jb = new JButton(pa);

            jb.setBackground(getColor(miResultSet.getString("color")));
            pnlIns.add(jb);
            pnlIns.revalidate();
            pnlIns.repaint();
        }
    }catch(Exception e){
        System.out.println(e);
    }

}

Button that performs the delete method:

private void eliminarInsActionPerformed(java.awt.event.ActionEvent evt) {                                            
    int filasel;
    String id;
    try 
    {
        filasel = tblInstaladores.getSelectedRow();
        if(filasel == -1)
        {
        JOptionPane.showMessageDialog(null, "No se ha seleccionado ninguna fila a eliminar");
        }
        else
        {
        modeloIns = (DefaultTableModel) tblInstaladores.getModel();    
        id = (String) modeloIns.getValueAt(filasel, 0);
        EliminarIns(id);
        }
        pnlIns.revalidate();
        pnlIns.repaint();
    } 
    catch (Exception e) 
    {
        JOptionPane.showMessageDialog(null, e);
    }
}

Method that eliminates:

void EliminarIns(String id){

    try{
        Connection miConexionInstaladores = DriverManager.getConnection("jdbc:sqlite:instaladores3Claro.db");
        PreparedStatement pst = miConexionInstaladores.prepareStatement("DELETE FROM instaladores3Claro "+
                "WHERE id = "+id);
        Statement miStatementIns = miConexionInstaladores.createStatement();

        int m = pst.executeUpdate();
        if(m > 0){
            CargarTablaInstaladores("");
        }



    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}

I hope you can help me:)

    
asked by Extibax 29.08.2018 в 17:42
source

1 answer

1

Add the index, since they are the same positions, also remove the other from the panel

private void eliminarInsActionPerformed(java.awt.event.ActionEvent evt) {                                            
        int filasel;
        String id;
        try 
        {
            filasel = tblInstaladores.getSelectedRow();
            if(filasel == -1)
            {
            JOptionPane.showMessageDialog(null, "No se ha seleccionado ninguna fila a eliminar");
            }
            else
            {
            modeloIns = (DefaultTableModel) tblInstaladores.getModel();    
            id = (String) modeloIns.getValueAt(filasel, 0);
            EliminarIns(id);
            }
            pnlIns.remove(filasel); //AQUÍ LO ELIMINAS
            pnlIns.revalidate();
            pnlIns.repaint();
        } 
        catch (Exception e) 
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }

the problem would be if your tables have sort, in that case, you should have more control when making the calls to the database, so that it is grouped all in a list and not in local calls.

EDIT: JPanel extends from Component that has the method remove(int index) , if your pnlIns as an array, {person1, person2} and you say pnlIns.remove(0) , it will remove the one from position 0 that is person1.

Also clarify that Component also contains a remove(Component component) where it eliminates the object that you pass, for example pnlIns.remove(persona2) will eliminate the object, although in your case it is difficult to apply it since all your objects are local, it is difficult to obtain the object as such, but it is important that you know that you can both form

More information about the remove method with Index link

Of the remove with component link

    
answered by 29.08.2018 / 18:55
source