2
    private void jButton_1ActionPerformed(java.awt.event.ActionEvent evt) {                                                

    String text = "";

    texto = RandomStringUtils.randomAlphanumeric(12);
    System.out.println(text);
    JOptionPane.showMessageDialog(null, text);

    }

I need to retrieve the variable String text

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

    String code, text = "";

    text = *??????*;   /*<-----This variable I need to retrieve the previous generated button*/
    code = txt_code.getText().trim();

    if (code.equals("")) {
        jLabel_codeerror.setText("This information is required.");
    } else {
        if (code.equals(text)) {
            jLabel_codeerror.setText("Equal code.");
        } else {
            jLabel_codeerror.setText("The confirmation has failed, please try again.");

               }
        }
    }

I do not know how to extract the variables from the other buttons, and I need since each button has different variables that later I have to gather them in the last button to update it

Thank you so much

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
wicked
  • 43
  • 9
  • Variable `text`, in method `jButton_1ActionPerformed` is a local variable. It does not exist outside of that method. Hence you cannot access it from a different method. @Babasile is suggesting, in his [answer](https://stackoverflow.com/questions/69144340/how-to-retrieve-string-value-call-from-existing-specific-jbutton-variable-in-jav/69144569#69144569), that you make `text` a class member variable which will let you access it from any method. – Abra Sep 11 '21 at 17:10
  • I get the impression that there is a better way to do what you are trying to do. I understand that you want to display some random text and get the user to enter the exact same text as a way of performing some kind of verification that the user is a human user and not some kind of robot. Is that correct? – Abra Sep 11 '21 at 17:13

2 Answers2

1

I solved it by creating the variable in my public class.

private String code = ""; 

So I can get the generated code and save it for later comparison, although I am afraid that anyone can see the generated code. since I will use it to encrypt the following information.

Gayan Mettananda
  • 1,498
  • 14
  • 21
wicked
  • 43
  • 9
0

You could use getter/setter to retrieve your value.

private String yourText;

public void setYourText(String yourText) {
   this.yourText = yourText;
}

public String getYourText() {
   return this.yourText ;
}

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


String text = "";

  texto = RandomStringUtils.randomAlphanumeric(12);
  System.out.println(text);
  JOptionPane.showMessageDialog(null, text);
  setYourText(text);

}

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

   String code, text = "";

   text = getYourText();

   code = txt_code.getText().trim();

   if (code.equals("")) {
      jLabel_codeerror.setText("This information is required.");
   } else {
      if (code.equals(text)) {
        jLabel_codeerror.setText("Equal code.");
      } else {
         jLabel_codeerror.setText("The confirmation has failed, please try again.");

      }
    }
}
Babasile
  • 360
  • 1
  • 6
  • 13