Member Avatar for jaredleo999
public class Welcome extends JFrame {
private Toolkit toolkit;
private static String passy = "123456";
public Welcome() 
{
    setSize(300,550);
    setTitle("Welcome");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    getContentPane().add(panel);
    panel.setLayout(null);  

    JLabel label = new JLabel("Enter Password");
    JPasswordField password = new JPasswordField(6);
    password.setEchoChar('*');
        password.addActionListener(new AL());
    label.setBounds(0,0,300,25);
        password.setBounds(100,0,200,25);

JButton b1 = new JButton("1");
b1.setBounds(0,100,100,100);
b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        "CONFUSED.COM"

    }});


panel.add(b1);


        panel.add(password);
    panel.add(label);
    panel.add(b1);
   setVisible(true);

}

Basically, when i click the JButton b1 (1) i want the digit to appear in the password field box, but i dont know the action event to do this!
any help is much appreciated!
Thanks
jaredleo999

You have to declare the password variable as final, and put the folowing line

password.setText(passy);

in the action performed in place of "CONFUSE.COM".
do not forget to add "}" at the end of you class, that is missed.
the full code follow.


import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;


public class Welcome extends JFrame {
private Toolkit toolkit;
private static String passy = "123456";
public Welcome()
{


setSize(300,550);
setTitle("Welcome");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);

JLabel label = new JLabel("Enter Password");
final JPasswordField password = new JPasswordField(6);
password.setEchoChar('*');
//password.addActionListener(new AL());
label.setBounds(0,0,300,25);
password.setBounds(100,0,200,25);

JButton b1 = new JButton("1");
b1.setBounds(0,100,100,100);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//"CONFUSED.COM"
password.setText(passy);
}});


panel.add(b1);


panel.add(password);
panel.add(label);
panel.add(b1);
setVisible(true);

}
}

Member Avatar for jaredleo999

Hi, thanks very much for your reply,
When i compile the program it says it needs to be declared final like you said, but when i do it i get error messages..
I think its best i show you all of my class:

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*; //For Formatting
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.lang.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;


public class Welcome extends JFrame {
private Toolkit toolkit;
private static String passy = "123456";
public Welcome() 
{
    setSize(300,550);
    setTitle("Welcome");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    getContentPane().add(panel);
    panel.setLayout(null);  

    JLabel label = new JLabel("Enter Password");
    JPasswordField password = new JPasswordField(6);
    password.setEchoChar('*');
        password.addActionListener(new AL());
    label.setBounds(0,0,300,25);
        password.setBounds(100,0,200,25);

JButton b1 = new JButton("1");
b1.setBounds(0,100,100,100);
b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {


    }});


   JButton b2 = new JButton("2");
b2.setBounds(100,100,100,100);
JButton b3 = new JButton("3");
b3.setBounds(200,100,100,100);
JButton b4 = new JButton("4");
b4.setBounds(0,200,100,100);
JButton b5 = new JButton("5");
b5.setBounds(100,200,100,100);
JButton b6 = new JButton("6");
b6.setBounds(200,200,100,100);
JButton b7 = new JButton("7");
b7.setBounds(0,300,100,100);
JButton b8 = new JButton("8");
b8.setBounds(100,300,100,100);
JButton b9 = new JButton("9");
b9.setBounds(200,300,100,100);
JButton b0 = new JButton("0");
b0.setBounds(100,400,100,100);
JButton Enter = new JButton("Enter");
Enter.setBounds(200,400,100,100);
JButton Clear = new JButton("Clear");
Clear.setBounds(0,400,100,100);


panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
panel.add(b7);
panel.add(b8);
panel.add(b9);
panel.add(b0);
panel.add(Enter);
panel.add(Clear);

        panel.add(password);
    panel.add(label);


}

   class AL implements ActionListener{
        public void actionPerformed(ActionEvent e) {
        JPasswordField input = (JPasswordField) e.getSource();
        char [] pass = input.getPassword();
        String p = new String(pass);
        if (p.equals(passy)){
            JOptionPane.showMessageDialog(null, "Correct");
            setVisible(false);
    dispose();
    ATM();
        }
        else 
            JOptionPane.showMessageDialog(null, "InCorrect");
        }
    }

     public void Keypad() {
 Keypad keypad = new Keypad();
 keypad.setVisible(true);
 }
public void ATM() {
 ATM atm = new ATM();
 atm.setVisible(true);  
}  
}

Basically i want to be able to type in the password '123456' using the buttons on the display..
Any help is appreciated thanks!

Hi
if you want to add the label's text button to the JPaswordField you have to add the action listeners to the buttons not to the JPaswordField.
It is recommanded to give a significatif name to your action listener in stead of AL I'v renamed it as EnterActionListener.
The solution i propose is very verbose but it's clear.
One more think could not see the KeyPad implementation nore the ATM.

the full code follows, just copy and past it and then let me now.

it work fine hope that could help.

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*; //For Formatting
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.lang.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class Welcome extends JFrame {

private Toolkit toolkit;
private static String passy = "123456";
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton Enter = new JButton("Enter");
JButton Clear = new JButton("Clear");
JPasswordField password = new JPasswordField(6);
JLabel label = new JLabel("Enter Password");

public Welcome() {
    initButtons();
    setSize(300, 550);
    setTitle("Welcome");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    getContentPane().add(panel);
    panel.setLayout(null);

    password.setEchoChar('*');
    Enter.addActionListener(new AL());
    label.setBounds(0, 0, 300, 25);
    password.setBounds(100, 0, 200, 25);
    b1.setBounds(0, 100, 100, 100);

    b2.setBounds(100, 100, 100, 100);
    b3.setBounds(200, 100, 100, 100);
    b4.setBounds(0, 200, 100, 100);
    b5.setBounds(100, 200, 100, 100);
    b6.setBounds(200, 200, 100, 100);
    b7.setBounds(0, 300, 100, 100);
    b8.setBounds(100, 300, 100, 100);
    b9.setBounds(200, 300, 100, 100);
    b0.setBounds(100, 400, 100, 100);
    Enter.setBounds(200, 400, 100, 100);
    Clear.setBounds(0, 400, 100, 100);


    panel.add(b1);
    panel.add(b2);
    panel.add(b3);
    panel.add(b4);
    panel.add(b5);
    panel.add(b6);
    panel.add(b7);
    panel.add(b8);
    panel.add(b9);
    panel.add(b0);
    panel.add(Enter);
    panel.add(Clear);
    panel.add(password);
    panel.add(label);
    setVisible(true);

}

private void initButtons() {
    b0.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doAction(b0);
        }
    });
    b1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doAction(b1);
        }
    });
    b2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doAction(b2);
        }
    });
    b3.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doAction(b3);
        }
    });
    b4.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doAction(b4);
        }
    });
    b5.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doAction(b5);
        }
    });
    b6.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doAction(b6);
        }
    });
    b7.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doAction(b7);
        }
    });
    b8.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doAction(b8);
        }
    });
    b9.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doAction(b9);
        }
    });
    Clear.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            password.setText("");
        }
    });
}

private void doAction(JButton button) {
    password.setText(String.copyValueOf(password.getPassword()) + button.getText());
}

class EnterActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        //JPasswordField input = (JPasswordField) e.getSource();
        char[] pass = password.getPassword();
        String p = new String(pass);
        if (p.equals(passy)) {
            JOptionPane.showMessageDialog(null, "Correct");
            setVisible(false);
            dispose();
//                ATM();
        } else {
            JOptionPane.showMessageDialog(null, "InCorrect");
        }
    }
}
/*public void Keypad() {
Keypad keypad = new Keypad();
keypad.setVisible(true);
}

public void ATM() {
ATM atm = new ATM();
atm.setVisible(true);
}*/
}

please change the line 53 to:
Enter.addActionListener(new EnterActionListener());

Member Avatar for jaredleo999

Hi
Thanks so much for your reply, ive got it all sorted now, here is an example of one of my buttons:

final  JButton b1 = new JButton("1");
b1.setBounds(0,100,100,100);
b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    password.setText(String.copyValueOf(password.getPassword()) + b1.getText());
    }});

Everything works perfectly!
The only thing i dont understand is how the Enter JButton works? Could you please explain what action is performed when this is pressed? The button it self works perfectly fine, but i am interested to know how :)
I'm new to java and want as much information as i can :)

Many thanks again!!
jaredleo999

the function excuted when you click on the action button is the actionPerformed defined in th following class:

class EnterActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        //JPasswordField input = (JPasswordField) e.getSource();
        char[] pass = password.getPassword();
        String p = new String(pass);
        if (p.equals(passy)) {
            JOptionPane.showMessageDialog(null, "Correct");
            setVisible(false);
            dispose();
//                ATM();
        } else {
            JOptionPane.showMessageDialog(null, "InCorrect");
        }
    }
}

to understand you can modify the code as following:
in stead of
Enter.addActionListener(new EnterActionListener());
you can do that:

   Enter.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            char[] pass = password.getPassword();
            String p = new String(pass);
            if (p.equals(passy)) {
                JOptionPane.showMessageDialog(null, "Correct");
                setVisible(false);
                dispose();
//                ATM();
            } else {
                JOptionPane.showMessageDialog(null, "InCorrect");
            }
        }
    });

good luck.

Member Avatar for jaredleo999

Hi there, hope all is well
Im going to start a new thread on this topic but if you can help here it would be greatly appreciated as you have been so helpful so far!

My project ATM needs an option at the welcome screen where the user can choose to have the numeric buttons in either black blue or red.
I'm not sure where to start with this, as i have many different classes like ATM(); and Balance(); where the buttons are not inherited, they are created within each class, so i am not sure of a function which can link all the jbuttons to a certain colour...
Any help is yet again appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.