java - Acess a variable from a method in a another method -


  /*  * change license header, choose license headers in project properties.  * change template file, choose tools | templates  * , open template in editor.  */  package controle_financeiro;  import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.statement;  /**  *  * @author iago  */  public class telacadastrocategoria extends javax.swing.jframe {      /**      * creates new form telacadastrocategoria      */     public telacadastrocategoria() {         initcomponents();     }       @suppresswarnings("unchecked")     // <editor-fold defaultstate="collapsed" desc="generated code">                               private void initcomponents() {          jlabel1 = new javax.swing.jlabel();         nomecategoria = new javax.swing.jtextfield();         botao_cadastra_tela_cadastro_categorias = new javax.swing.jbutton();         botao_voltar_tela_cadastro_categorias = new javax.swing.jbutton();          setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close);          jlabel1.setfont(new java.awt.font("tahoma", 0, 18)); // noi18n         jlabel1.sethorizontalalignment(javax.swing.swingconstants.center);         jlabel1.settext("nome da categoria");          nomecategoria.addmouselistener(new java.awt.event.mouseadapter() {             public void mouseclicked(java.awt.event.mouseevent evt) {                 nomecategoriamouseclicked(evt);             }         });          botao_cadastra_tela_cadastro_categorias.settext("cadastrar");         botao_cadastra_tela_cadastro_categorias.addmouselistener(new java.awt.event.mouseadapter() {             public void mouseclicked(java.awt.event.mouseevent evt) {                 botao_cadastra_tela_cadastro_categoriasmouseclicked(evt);             }         });          botao_voltar_tela_cadastro_categorias.settext("voltar");         botao_voltar_tela_cadastro_categorias.addmouselistener(new java.awt.event.mouseadapter() {             public void mouseclicked(java.awt.event.mouseevent evt) {                 botao_voltar_tela_cadastro_categoriasmouseclicked(evt);             }         });         botao_voltar_tela_cadastro_categorias.addactionlistener(new java.awt.event.actionlistener() {             public void actionperformed(java.awt.event.actionevent evt) {                 botao_voltar_tela_cadastro_categoriasactionperformed(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(98, 98, 98)                 .addcomponent(jlabel1, javax.swing.grouplayout.preferred_size, 191, javax.swing.grouplayout.preferred_size)                 .addcontainergap(javax.swing.grouplayout.default_size, short.max_value))             .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup()                 .addcontainergap(76, short.max_value)                 .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing, false)                     .addgroup(layout.createsequentialgroup()                         .addcomponent(botao_voltar_tela_cadastro_categorias, javax.swing.grouplayout.preferred_size, 87, javax.swing.grouplayout.preferred_size)                         .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, javax.swing.grouplayout.default_size, short.max_value)                         .addcomponent(botao_cadastra_tela_cadastro_categorias, javax.swing.grouplayout.preferred_size, 110, javax.swing.grouplayout.preferred_size))                     .addcomponent(nomecategoria, javax.swing.grouplayout.preferred_size, 250, javax.swing.grouplayout.preferred_size))                 .addgap(74, 74, 74))         );         layout.setverticalgroup(             layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)             .addgroup(layout.createsequentialgroup()                 .addgap(50, 50, 50)                 .addcomponent(jlabel1, javax.swing.grouplayout.preferred_size, 45, javax.swing.grouplayout.preferred_size)                 .addgap(40, 40, 40)                 .addcomponent(nomecategoria, javax.swing.grouplayout.preferred_size, 45, javax.swing.grouplayout.preferred_size)                 .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, 52, short.max_value)                 .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline)                     .addcomponent(botao_cadastra_tela_cadastro_categorias, javax.swing.grouplayout.preferred_size, 43, javax.swing.grouplayout.preferred_size)                     .addcomponent(botao_voltar_tela_cadastro_categorias, javax.swing.grouplayout.preferred_size, 43, javax.swing.grouplayout.preferred_size))                 .addgap(25, 25, 25))         );          pack();     }// </editor-fold>                              int var_id=1;      private void botao_cadastra_tela_cadastro_categoriasmouseclicked(java.awt.event.mouseevent evt) {                                                                           try {                string url = "jdbc:postgresql://localhost:5432/db_controle_financeiro";               string usuario = "us_iago";               string senha = "123";                class.forname("org.postgresql.driver");                connection con;                con = drivermanager.getconnection(url, usuario, senha);               system.out.println("conexão realizada com sucesso.");               statement stm = con.createstatement();              stm.executeupdate("insert categoria (id,nome) values ('" + var_id + "','" + nome_categoria + "'");             var_id++;               con.close();           } catch (classnotfoundexception | sqlexception e) {               system.out.println("problemas ao conectar ao banco.");         }             telainicial tela_inicial_ccategoria = new telainicial();         tela_inicial_ccategoria.setvisible(true);           dispose();     }                                                                           private void nomecategoriamouseclicked(java.awt.event.mouseevent evt) {                                                     string nome_categoria = (nomecategoria.gettext());      }                                                  public static void main(string args[]) {           java.awt.eventqueue.invokelater(new runnable() {             public void run() {                 new telacadastrocategoria().setvisible(true);             }         });     }      // variables declaration - not modify                          private javax.swing.jbutton botao_cadastra_tela_cadastro_categorias;     private javax.swing.jbutton botao_voltar_tela_cadastro_categorias;     private javax.swing.jlabel jlabel1;     public javax.swing.jtextfield nomecategoria;     // end of variables declaration                    } 

i'm making financial control program in java, i'm having problems acess variable in method method. how can acess variable nome_categoria method nomecategoriamouseclicked in method botao_cadastra_tela_cadastro_categoriasmouseclicked?

you can make variable instance variable. instead of declaring in method, put declaration/assignment @ top. can access anywhere in class.

public class telacadastrocategoria extends javax.swing.jframe {     string nome_categoria;     // other code goes here } 

you able use in method of class.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -