I want the JButtons to display their respective numbers on the JPasswordField (I know I'll only see the password dots). I'm not even sure why its not displaying it now, what am I missing? Here I show the program running: (short gif, no need to download) http://gyazo.com/b3fec4f8e433b05274cb0af78777ece6
package tarea10;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Tarea10 {
private JFrame f;
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12;
private JPasswordField passField;
private JPanel p;
public Tarea10() {
p = new JPanel();
passField = new JPasswordField("");
f = new JFrame("Programa");
b1 = new JButton("7");
b2 = new JButton("8");
b3 = new JButton("9");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("1");
b8 = new JButton("2");
b9 = new JButton("3");
b10 = new JButton("Cancelar");
b11 = new JButton("0");
b12 = new JButton("Aceptar");
}
ActionListener al = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JButton button = (JButton) e.getSource();
String prev = passField.getText();
passField.setText(prev + button.getText());
b1.addActionListener(al);
b2.addActionListener(al);
b3.addActionListener(al);
b4.addActionListener(al);
b5.addActionListener(al);
b6.addActionListener(al);
b7.addActionListener(al);
b8.addActionListener(al);
b9.addActionListener(al);
b11.addActionListener(al);
}
};
public void mostrar() {
p.setLayout (new GridLayout(4,3));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(passField);
f.add(passField, BorderLayout.WEST);
f.add(p, BorderLayout.EAST);
f.setSize(450,400);
f.setVisible(true);
}
public static void main(String args[]) {
Tarea10 ventana = new Tarea10();
ventana.mostrar();
}}
you should use here is the corrected code