swap the text of a label with values of a java array

Asked

Viewed 89 times

0

I’m studying about vectors, and I want to create a program that, by typing a password(only letters) in the text field and pressing a button, the program would exchange the text of the password label for what I typed in the textfield, but I’m not able to think of a way to put the letters of the password vector in lbpassword

inserir a descrição da imagem aqui

b.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                String s = tf.getText();
                String[] senha = new String[s.length()];
                //letras
                for (char letra = 'a'; letra <= 'z'; letra++){
                    //percorrer as letras da senha
                    for (int i = 0; i == s.length(); i++) {
                        //para cada espaço no vetor adiciona uma letra do txtfield
                        senha[i] = String.valueOf(s.charAt(i));
                        //se a letra percorrida for igual a letra do vetor senha[]
                        if(senha[i] == String.valueOf(letra)){
                            //senha recebe a letra
                            senha[i] = String.valueOf(letra);
                            //trocar o texto do label pela senha
                            lbsenha.setText(senha[i]+senha[i++]);
                        }

                    }
                }
            }

        });

1 answer

1


You can use the method append class StringBuilder to concatenate new characters, and then at the end set the complete string in the label.

Example:

StringBuilder senha = new StringBuilder(); // instância no escopo da classe

public void mouseClicked(MouseEvent e) {
 super.mouseClicked(e);
 String s = tf.getText();

 for (char letra = 'a'; letra <= 'z'; letra++) {
  for (int i = 0; i == s.length(); i++) {
   if (s.charAt(i) == letra) {
    this.senha.append(letra);
    lbsenha.setText(this.senha);
   }
  }
 }
}

Browser other questions tagged

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