Home > Blockchain >  How to get data from a jTextField or jFormattedTextField in another java class
How to get data from a jTextField or jFormattedTextField in another java class

Time:10-20

I'm trying to use to different classes on netbeans, one is my GUI (also, i'm starting at programming and just learned what is one GUI), and in this one, I have some jTextField's and JFormattedTextField's, the other one, I want to use as back-end, for starters, I want to delete a information that I already have on my DB.

The error I get is this:

error: cannot find symbol if(jFormattedTextFieldCPF.getText().equals("")){ symbol: variable jFormattedTextFieldCPF location: class Modos

Here's the first part of my code:

private void jButtonExcluirActionPerformed(java.awt.event.ActionEvent evt) {                                               

    
    MODO = 1;// it sends which action the button have to do
    funcao.funcoes(MODO);
    
    

    // TODO add your handling code here:

}

In the class Modos I only have a function called funcoes, where there will be more actions to do on my DB, like, edit, add, search...

public void funcoes(int MODO) {
    
    if (MODO == 1){
        
                PreparedStatement stm;
    try {
        /*jFormattedTextFieldCPF.setEnabled(true);
        jTextFieldNOME.setEnabled(false);
        jTextFieldIDADE.setEnabled(false);
        jFormattedTextFieldDATA.setEnabled(false);
        jTextFieldAPELIDO.setEnabled(false);
                    */
        stm = conecta.conn.prepareStatement("delete from cad_pessoa where cad_cpf=?");
        
        
        if(jFormattedTextFieldCPF.getText().equals("")){
            
            JOptionPane.showMessageDialog(null, "Por favor completar o campo CPF");
            conecta.conn.rollback();
        }
        
        else {
            
        
        stm.setString(1, jFormattedTextFieldCPF.getText()); //pega o nome que será deletado
        stm.execute(); //executa o SQL
        conecta.conn.commit();
        
        JOptionPane.showMessageDialog(rootPane, "Excluído!");
        
        /*jFormattedTextFieldCPF.setText("");
        jTextFieldNOME.setText(""); //deixa o campo vazio
        jTextFieldIDADE.setText("");
        jFormattedTextFieldDATA.setText("");//deixa o campo vazio
        jTextFieldAPELIDO.setText(""); //deixa o campo vazio
        
        jFormattedTextFieldCPF.setEnabled(false); //deixa o campo indisponivel
        jTextFieldNOME.setEnabled(false);
        jTextFieldIDADE.setEnabled(false);
        jFormattedTextFieldDATA.setEnabled(false);
        jTextFieldAPELIDO.setEnabled(false);
        jButtonINSERIR.setEnabled(true);
        jButtonALTERAR.setEnabled(false);*/
        
        // as a comentary cuz everything is a jFrame and gives an error
        }

    } catch (SQLException ex) {
        Logger.getLogger(Pessoa.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null, "Erro:01\n"   ex.getMessage());
        
    }
        
    }

Sorry for the long question, To simplify, I want to get the info on the jFormattedTextField in another class.

CodePudding user response:

You can declare a static variable of type jTextField, inside your method initialize this static variable with the same object jFormattedTextField after modifications, declare a static get method to get this variable from another class and call it.

 class WhereYourJTextExists{
  private  jTextField jText ;
  private static jTextField jToPass;

  public static  jTextField getJToPass(){
   return jToPass;
  }
  public void yourTaskHere(){
  // .... do some tasks with jText
  // initialize your variable with the jTextField 
  jToPass= jText;
  }
  }
  // ----------------------------------
  class OtherClass{
  public void needJtextHere(){
  jTextField recievedJtext = WhereYourJTextExists.getJToPass();
  //...do your job with the recived jTextField 
  }
  }

CodePudding user response:

Decouple your code. Pass all the information that funcoes needs in order to do it's job and have it throw one or more exceptions as needed, for example...

public void funcoes(int MODO, String cpf) throws SQLException {

    if (MODO == 1) {
        if (cpf.equals("")) {
            // This could be a custom exception, which would make it easier 
            // to detect and display a custom error message
            throw new SQLException("Por favor completar o campo CPF");
        } else {
            try (PreparedStatement stm = conecta.conn.prepareStatement("delete from cad_pessoa where cad_cpf=?")) {
                stm.setString(1, jFormattedTextFieldCPF.getText()); //pega o nome que será deletado
                stm.execute(); //executa o SQL
                conecta.conn.commit();
            }
        }
    }
}
  • Related