0

No tengo mucho conocimiento en CRUD MVC y para aprender decidí seguir una serie de vídeos respecto al tema. En los vídeos solo crean un JFrame Usuarios en la Vista y de allí surge mi problema, ya que le agregue un JFrame para un Inicio(Home), desde el JFrame Inicio agregue un JButton para llamar al JFrame Usuarios, pero al hacer uso del JButton si me abre el JFrame Usuarios pero los JButton del JFrame Usuarios que son para insertar,actualizar y eliminar en una base de datos ya no funcionan. introducir la descripción de la imagen aquí

El Main Class es JFrame Inicio

Al ejecutar directamente el JFrame Usuarios funciona todo sin problemas. Espero alguien me ayude lo más pronto posible. Desde ya agradecido por la ayuda. adjunto código.

//---Codigo JFrame Usuarios--\

public class frmUsuarios extends javax.swing.JFrame {
    //static Connection cn = null;
    static ResultSet rs = null;
    PreparedStatement ps = null;

   Conexion con = new Conexion();//instanciar clase Conexion
   Connection cn = con.getConexion(); //obtener la conexion

public frmUsuarios() throws SQLException {
    initComponents();

    try {
        String sql = ("Select * from tipo_empleado");
        PreparedStatement ps = cn.prepareStatement(sql);
        rs=ps.executeQuery();

        while(rs.next()){
            jComboTipoUsuario.addItem(rs.getString("Cod_tipo_empleado"));
        }
        rs.close();
    } catch (SQLException ex) {

    }


}

 private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {                                            
    //eliminar datos de jtable al eliminar un registro de la base de datos
    try {

    int fila = jtUsuarios.getSelectedRow();
    String codigo = jtUsuarios.getValueAt(fila, 0).toString();
    DefaultTableModel modelo = (DefaultTableModel)jtUsuarios.getModel();      

    ps.setString(1, codigo);
    modelo.removeRow(fila);
    } catch (Exception e) {
    }
}                     

public static void main(String args[]) throws SQLException {
Usuarios mod = new Usuarios();
    ConsultasUsuarios modC = new ConsultasUsuarios();
    frmUsuarios frm = new frmUsuarios();

    CtrlUsuarios ctrl = new CtrlUsuarios(mod, modC, frm);
    ctrl.iniciar();
    frm.setVisible(true);

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {

        }
    });
}

//---Codigo JFrame Inicio---\

//declarar variables de tipo Login y frmUsuarios
public static Login frmLog;
public static frmUsuarios frmReg;

public Inicio() {
    initComponents();
}

/**
 * This method is called from within the constru
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setText("Registrar");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setText("Ingresar");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(73, 73, 73)
            .addComponent(jButton1)
            .addGap(56, 56, 56)
            .addComponent(jButton2)
            .addContainerGap(121, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(105, 105, 105)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1)
                .addComponent(jButton2))
            .addContainerGap(172, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        ctor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setText("Registrar");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setText("Ingresar");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(73, 73, 73)
            .addComponent(jButton1)
            .addGap(56, 56, 56)
            .addComponent(jButton2)
            .addContainerGap(121, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(105, 105, 105)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1)
                .addComponent(jButton2))
            .addContainerGap(172, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if (frmReg == null) {

        try {
            frmReg = new frmUsuarios();
        } catch (SQLException ex) {
            Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
        }

        frmReg.setVisible(true);
    }
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if (frmLog == null) {

        frmLog = new Login();
        frmLog.setVisible(true);
    }
}     

public static void main(String args[]) throws SQLException {
try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }


    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Inicio().setVisible(true);
        }
    });
}

1 respuesta 1

0

Los botones deben estar declarados en la.clase correspondiente, a menos que extiendas de la clase en la que están declarados.

1
  • gracias por la ayuda, y disculpa la molestia, pero me podrías indicar como seria mas o menos el código. el 24 may. 2020 a las 18:45

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.