Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: password denied

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default password denied

    i cant figure this out please help ...

    main class that runs the password prompt
    public class PasswordField extends JFrame {
     
        private JPanel passwordPanel;
     
        private JPasswordField passwordField;
     
        private JLabel passwordLabel;
     
        private JButton userConfirmationBtn;
        private JButton submenu;
     
        private Image icon;
     
        private ImageIcon frameBackground;
        private ImageIcon buttonBackground;
     
        private String passwordAuthentication;
     
        public PasswordField() {
     
            initComponents();
        }
     
        private void initComponents() {
     
            icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0);
            frameBackground = new ImageIcon("c:/pics/passwordbck2.jpg");
            buttonBackground = new ImageIcon("c:/pics/buttonbck");
            passwordField = new JPasswordField();
            passwordLabel = new JLabel("Password: ");
            userConfirmationBtn = new JButton("Enter");
            submenu = new JButton("Submenu");
     
            // set the properties of this label
            passwordLabel.setBounds(12, 13, 80, 20);
            passwordLabel.setFont(new Font("Monospaced", Font.BOLD, 12));
            passwordLabel.setForeground(Color.BLACK);
     
     
            // set the properties and actions of this text field
            passwordField.setBounds(88, 13, 185, 20);
     
            // set the properties and actions of this button
            Action pass = new AbstractAction() {
     
                public void actionPerformed(ActionEvent e) {
     
                    Password p = new Password();
                    // retrive the password data from the password field
                    char[] passwordChars = passwordField.getPassword();
     
                    String passwordStr = "";
     
                    // convert the returned password characters as string
                    for (int x = 0; x <= passwordChars.length - 1; x++) {
     
                        passwordStr = passwordStr + passwordChars[x];
                    }
                    // check if the password entered is valid
     
                    if (p.isPasswordAuthenticated(passwordStr)) {
     
                     //   new InventoryDatabase();
                        disableWindow();
                    }
                    else if (passwordStr.equals("")) {
     
                       // PopUpDialog.popTheErrorMessage(" Unauthorized Entry", "Please Fill The Specified Field.");
                    }
                    else {
     
                     //   PopUpDialog.popTheErrorMessage(" Unauthorized Entry !", "Access Denied.");
                    }
                }
            };
            userConfirmationBtn.addActionListener(pass);
            userConfirmationBtn.setMnemonic(KeyEvent.VK_E);
            userConfirmationBtn.setBounds(88, 55, 90, 20);
            userConfirmationBtn.setFont(new Font("Monospaced", Font.BOLD, 11));
            userConfirmationBtn.setBackground(Color.LIGHT_GRAY);
            userConfirmationBtn.registerKeyboardAction(userConfirmationBtn.getActionForKeyStroke(
                                                       KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
                                                       KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
                                                       JComponent.WHEN_IN_FOCUSED_WINDOW);
            userConfirmationBtn.registerKeyboardAction(userConfirmationBtn.getActionForKeyStroke(
                                                       KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
                                                       KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
                                                       JComponent.WHEN_IN_FOCUSED_WINDOW);
     
            // set the properties and actions of this button
            Action openSubmenu = new AbstractAction() {
     
                public void actionPerformed(ActionEvent e) {
     
                    Submenu.openSubmenu();
                }
            };
            submenu.addActionListener(openSubmenu);
            submenu.setMnemonic(KeyEvent.VK_S);
            submenu.setBounds(184, 55, 90, 20);
            submenu.setFont(new Font("Monospaced", Font.BOLD, 11));
            submenu.setBackground(Color.LIGHT_GRAY);
     
     
            passwordPanel = new JPanel() {
     
                @Override
                protected void paintComponent(Graphics g) {
     
                    // Scale image to size of component
                    Dimension d = getSize();
     
                    g.drawImage(frameBackground.getImage(), 0, 0, d.width, d.height, null);
     
                    super.paintComponent(g);
                }
            };
            passwordPanel.setOpaque(false);
     
            // set this panel's layout to null for absolute positioning of
            // components
            passwordPanel.setLayout(null);
     
            // add the components to this panel
            passwordPanel.add(passwordLabel);
            passwordPanel.add(passwordField);
            passwordPanel.add(userConfirmationBtn);
            passwordPanel.add(submenu);
     
            // add the panel to the container
            getContentPane().add(passwordPanel);
     
            setTitle(" User Confirmation");
            setIconImage(icon);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(288, 120);
            setResizable(false);
            setLocationRelativeTo(null);
            setVisible(true);
        }
     
        /**
         * Invoke this method if this window doesnt have any more use for the current
         * event.
         */
        private void disableWindow() {
     
            setVisible(false);
        }
     
        public static void main(String[] args) {
     
            SwingUtilities.invokeLater(new Runnable() {
     
                public void run() {
     
                    new PasswordField();
                }
            });
        }
    }

    password class, not fully functional
    public class Password {
     
        private String[] passwords;
     
        public Password() {
     
            passwords = new String[] {"123,","ABC"};
        }
     
        public boolean isPasswordAuthenticated(String 
     
            for (int x = 0; x <= passwords.length - 1; x++) {
     
                if (password.equals(passwords[x])) {
     
                    return true;
                }
                else {
     
                    return false;
                }
            }
     
     
            return false;
        }
     
        public static void addNewPassword(String newPassword) {
     
            // UNSUPPORTED
            throw new UnsupportedOperationException();
        }
     
        public static void deletePassword(String password) {
     
            // UNSUPPORTED
            throw new UnsupportedOperationException();
        }
    }

    i cant figure out why does the password is denied? im trying to enter the exact password data...
    Last edited by chronoz13; January 28th, 2010 at 12:22 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: password denied

    Your authentication function only checks the first password in the password array

        public boolean isPasswordAuthenticated(String  password)        
            for (int x = 0; x <= passwords.length - 1; x++) {
                if (password.equals(passwords[x])) {
                    return true;
                }
                //else {
                //
                //   return false; //If this is here, it will return false after only checking the first password
                //}
            }       
            return false;
        }

  3. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (January 28th, 2010)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: password denied

    sir I cant figure it out why does it only checks the first password in the array...?

    so if it is , then if i enter "123", it will still return true....

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: password denied

    public class Password {
     
        private String[] passwords;
     
        public Password() {
     
            passwords = new String[] {"123", "ABC"};
        }
     
        public  boolean isPasswordAuthentic(String password) {
     
            boolean isAuthentic = false;
     
            for (int x = 0; x <= passwords.length - 1; x++) {
     
                if (password.equals(passwords[x])) {
     
                    isAuthentic = true;
                    break;
                }
            }
     
            return isAuthentic;
        }
     
        public static void addNewPassword(String newPassword) {
     
            // UNSUPPORTED
            throw new UnsupportedOperationException();
        }
     
        public static void deletePassword(String password) {
     
            // UNSUPPORTED
            throw new UnsupportedOperationException();
        }
    }

    i solved it sir.... thank you!
    Last edited by chronoz13; January 28th, 2010 at 09:30 PM.

Similar Threads

  1. Find the password with dictionary attack
    By mortis1572 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 19th, 2010, 10:07 PM
  2. [SOLVED] Password screens
    By Dave in forum AWT / Java Swing
    Replies: 7
    Last Post: August 26th, 2009, 06:37 AM
  3. password
    By 5723 in forum Algorithms & Recursion
    Replies: 9
    Last Post: July 9th, 2009, 05:26 AM
  4. [SOLVED] java password
    By pwngrammer in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 15th, 2009, 09:49 AM
  5. Java password generator program to generate 8 random integers
    By Lizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 16th, 2009, 07:49 AM