Results 1 to 2 of 2
  1. #1
    Chick786 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Exclamation JPasswordField question..

    OK - This bit baffled me..

    private static class JPassword implements ActionListener {
    String password = "pass"; //string containing the password
    private void main() {
    //create frame
    JFrame frame = new JFrame("Password");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(400,100);
    JButton B1 = new JButton("Check Balance");



    JLabel label3 = new JLabel("Enter Pin");
    JPanel panel = new JPanel();
    frame.add(panel);

    JPasswordField pass = new JPasswordField(5);
    pass.setEchoChar('*'); //actionlistener

    panel.add(label3, BorderLayout.WEST);
    panel.add(pass, BorderLayout.WEST);


    }

    public void actionPerformed(ActionEvent e){
    JPasswordField input = (JPasswordField) e.getSource();
    char[] passy = input.getPassword();
    String p = new String(passy);
    if (p.equals(password)){
    JOptionPane.showMessageDialog(null, "Correct");
    }
    else
    JOptionPane.showMessageDialog(null, "Incorrect");
    }

    }
    }

    Have the above code.. works.. I've created a button... with an Action Listener... How do I join these two?? Is it done in the code above or in a seperate code?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm surprised that your code works. You're adding things to a JFrame after setting it to visible, and you're not adding the JButton at all. I suggest you hit the Sun Swing tutorials as it will show you the right way to do this.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. Any workarounds for JPasswordField in JDialog bug?
    By dtmansy in forum Advanced Java
    Replies: 5
    Last Post: 02-24-2010, 12:56 AM
  3. Ask about JPasswordfield
    By yonghan in forum AWT / Swing
    Replies: 7
    Last Post: 09-14-2008, 12:54 PM
  4. a question
    By slytheman in forum Java Servlet
    Replies: 0
    Last Post: 03-12-2008, 05:11 AM
  5. JNI question
    By javaplus in forum New To Java
    Replies: 0
    Last Post: 12-24-2007, 11:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •