I have a variable set by parameter. How do I use it on buttons (Action Performed)?

Asked

Viewed 27 times

-1

I received this variable (per parameter) from another Jframe:

public App(int id) {
        System.out.println("teste: "+id);
        /* codigo aqui... */ 
}

and wanted to use inside that 'action performed button':

private void MostrarActionPerformed(java.awt.event.ActionEvent evt) {
        int local = id;
        /* codigo aqui... */ 
    }

How do I do it? I pass it to Static inside the . class? but how is it done?

2 answers

0

Remember that a Jframe is nothing more than a class. This class can have attributes declared at its beginning even before its constructor.. So an easy way to keep this "id" passed to this Jframe would be to have a variable outside the constructor.

example:

public class UmFrameQualquer() extends javax.swing.JFrame {
    int id;  //variável criada para alocar o valor "id" que virá externamente
    public UmFrameQualquer(int id_){
        initComponents();
        id = id_;  //nesse momento o valor "id_" ja está salvo na nossa variável podendo ser acessada em qualquer lugar da nossa JFrame
    }

}

0

I ended up learning to use Action Listener and Action Performed. :) solved. being like this:

public App(int id) {
  jButton.AddActionListener(...){
    Action Performed(){
       /*ação do botão com o 'id'*/
    }
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.