how to update a jtable from another jframe

0

How can I update the table when I modify another jframe. Is it possible?

I have a form in java Netbeans in which I insert data and modify, the action of modifying it in a popup window by clicking on a row of a table, up to here everything is fine but when I modify it in the popup window I update with a method that is in the interface where the jtable is but it does not work. I leave the code below

    //este metodo lo llamo del jframe 
    public void datos(String opcion){
 DefaultTableModel modelo= new DefaultTableModel();
   modelo.addColumn("rut");
   modelo.addColumn("nombre");
   modelo.addColumn("A.materno");
   modelo.addColumn("A.paterno");
   modelo.addColumn("Fono");
   modelo.addColumn("Nro °casa");
   modelo.addColumn("Calle");
   modelo.addColumn("Sector");
   modelo.addColumn("Comuna");
   modelo.addColumn("T.zapato");
   modelo.addColumn("T.pantalon");
   modelo.addColumn("T.chaqueta");
   modelo.addColumn("Estado");
   modelo.addColumn("Paswword");
   modelo.addColumn("Cargo");
   modelo.addColumn("id_co");
   modelo.addColumn("id_e");
   modelo.addColumn("id_ca");

  ResultSet rs=null;

  try{
    if(opcion=="todo"){
  rs=obj.mostrar();
  }
   else if(opcion=="buscar"){

       obj.setNombre(txtbuscar.getText());
       obj.setRut(txtbuscar.getText());
       obj.setOpcion(buttonGroup1.getSelection().getActionCommand());
   rs=obj.buscar();
  }
    if(rs.next()){
  while(rs.next()){
    Object [] fila= new Object[18];  
    fila[0]=rs.getString("RUT_TRABAJADOR");
    fila[1]=rs.getString("NOMBRE");
    fila[2]=rs.getString("APELLIDO_M");
    fila[3]=rs.getString("APELLIDO_P");
    fila[4]=rs.getInt("TELEFONO");
    fila[5]=rs.getInt("NRO_CALLE");
    fila[6]=rs.getString("CALLE");
    fila[7]=rs.getString("VILLA_POBLACION");
    fila[8]=rs.getString(9);
    fila[9]=rs.getInt("TALLA_CALZADO");
    fila[10]=rs.getInt("TALLA_PANTALON");
    fila[11]=rs.getInt("TALLA_CHAQUETA");
    fila[12]=rs.getString("NOMBRE_ESTADO");
    fila[13]=rs.getString("password");
    fila[14]=rs.getString("NOMBRE_CARGO");
    fila[15]=rs.getString("ID_COMUNA");
    fila[16]=rs.getString("ID_ESTADO");
    fila[17]=rs.getString("ID_CARGO");
    modelo.addRow(fila);
   }
  }

  }catch(Exception e){
      System.out.println(e.getMessage());
  }
tabla.setModel(modelo);





}




 //click del boton de la ventana emergente donde modifico y llamo a la funcion datos que esta en el jframe principal

private void btnmodificarMouseClicked(java.awt.event.MouseEvent evt) {                                          
     Mantenedor_insumo m= new Mantenedor_insumo();
     //modificar datos del trabajador


    try{
        obj.setRut(txtrut.getText());
        obj.setNombre(txtnombre.getText());
        obj.setApellido_m(txtapellido_m.getText());
        obj.setApellido_p(txtapellido_p.getText());
        obj.setCalzado(Integer.parseInt(txtzapato.getText()));
        obj.setPantalon(Integer.parseInt(txtpantalon.getText()));
        obj.setChaqueta(Integer.parseInt(txtchaqueta.getText()));
        obj.setFono(Integer.parseInt(txtfono.getText()));
        obj.setNro_calle(Integer.parseInt(txtnro_casa.getText()));
        obj.setCalle(txtcalle.getText());
        obj.setSector(txtsector.getText());
        String  id=combocomuna.getSelectedItem().toString();
        String [] comuna= id.split("-");
        obj.setId_comuna(Integer.parseInt(comuna[0].trim()));
        String id_cargo=lst_cargo.getSelectedItem().toString();
        String [] cargo=id_cargo.split("-");
        obj.setId_cargo(Integer.parseInt(cargo[0].trim()));
        String id_estado=lst_estado.getSelectedItem().toString();
        String [] estado=id_estado.split("-");
        obj.setId_estado(Integer.parseInt(estado[0].trim()));
        obj.setPassword(txtpass.getText());
        obj.modificar();
        JOptionPane.showMessageDialog(null, "Datos del trabajador modificado correctamente","Confirmacion",JOptionPane.INFORMATION_MESSAGE);
        m.datos("todo");
    }catch(Exception e){
        System.out.println("problema en modificar : " + e.getMessage()+ " ");

    }

}       


 //metodo modificar en clase trabajador funciona correctamente
 public void modificar () throws SQLException{
 String sql="UPDATE trabajador set        NOMBRE=?,APELLIDO_M=?,APELLIDO_P=?,TELEFONO=?,CALLE=?,NRO_CALLE=?,"
      + "VILLA_POBLACION=?,TALLA_CALZADO=?,TALLA_PANTALON=?,TALLA_CHAQUETA=?,password=?,"
      + "ID_COMUNA=?,ID_ESTADO=?,ID_CARGO=? WHERE RUT_TRABAJADOR=?";
Connection cn=datasource.getConnection();
PreparedStatement sentencia=cn.prepareStatement(sql);
sentencia.setString(1,getNombre());
sentencia.setString(2,getApellido_m());
sentencia.setString(3,getApellido_p());
sentencia.setInt(4,getFono());
sentencia.setString(5,getCalle());
sentencia.setInt(6,getNro_calle());
sentencia.setString(7,getSector());
sentencia.setInt(8,getCalzado());
sentencia.setInt(9,getPantalon());
sentencia.setInt(10,getChaqueta());
sentencia.setString(11,getPassword());
sentencia.setInt(12, getId_comuna());
sentencia.setInt(13, getId_estado());
sentencia.setInt(14, getId_cargo());
sentencia.setString(15, getRut());

sentencia.executeUpdate();


    System.out.println("sql modificar: "+ sentencia);



}
    
asked by jose miguel jara 21.02.2017 в 22:48
source

1 answer

1

for the next one that happens, build a button to update the frame that receives the action and from the frame that makes the action, we link it with nameframe.nombreboton.doClick (); behind the button we insert the table or the one that is going to update and already

    
answered by 24.08.2018 в 05:11