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 8 of 8

Thread: Password screens

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Password screens

    hello

    i am currently looking at how to place a password screen or box and the beginning and end of a program. this is to give a Sign In/ Sign Out effect.

    any ideas on how i can do this

    Thanks

    Dave


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Password screens

    Hello Dave, now thats a rather vague question I think, what kind of program is this, are you using Swing or is it purely a console program?

    // Json

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Password screens

    a console program

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Password screens

    Hello Dave,

    Here is an example of a password prompt using Java Swing



    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    /**
     * JavaProgrammingForums.com
     * Spread the word ;)
     */
     
    public class Password{
     
        // Set password here
        final static String pwd = "javaisthebest";
     
        JPanel panel;
        JTextField myPwd = new JTextField(20);
        final static String BUTTONPANEL = "";
     
     
        public void PasswordCheck(){
     
            String enteredPass = myPwd.getText();
     
            if(enteredPass.trim().equals(pwd)){
                System.out.println("Correct Password!!");        
            }
            else{
                System.out.println("Wrong Password. Try again.");
            }
        }
     
        public void addComponentToPane(Container pane) {
     
            JPanel panel1 = new JPanel();
            panel1.add(myPwd);
     
            JButton button = new JButton("Login");
            panel1.add(button);        
            button.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    PasswordCheck();
                }
            });      
     
            panel = new JPanel(new CardLayout());
            panel.add(panel1, BUTTONPANEL);
            pane.add(panel, BorderLayout.CENTER);
        }
     
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("Password Prompt");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            Password password = new Password();
            password.addComponentToPane(frame.getContentPane());
     
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
     
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    Dave (August 26th, 2009)

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Password screens

    Hello again Dave!

    I missed your post saying you wanted it to be a console application. How about this?

    import java.util.Scanner;
     
    public class PasswordConsole {
     
        /**
         * JavaProgrammingForums.com
         * Spread the word ;)
         */
     
        final static String password = "javaisthebest";
     
        public static void main(String[] args) {
     
            Scanner sc = new Scanner(System.in);
            boolean on = true;
            int count = 0;
     
            System.out.println("This is a restricted area!");
     
            while(on){
            System.out.println("Enter your password: ");
     
            String enteredPass = sc.nextLine();
     
            if(enteredPass.equals(password)){
                System.out.println("Password Correct! Welcome Dave. ");
                on = false;
            }else{
                if(count == 2){
                    System.out.println("BOOOOOOOOOOOOOOOOOOM");
                    System.exit(0);
                }
                System.out.println("Wrong Password! System will self destruct after 3 failed attempts.");
                count++;
                }
            }
        }
    }

    Example output:

    This is a restricted area!
    Enter your password:
    javaisthebest
    Password Correct! Welcome Dave.
    This is a restricted area!
    Enter your password:
    javaisbad
    Wrong Password! System will self destruct after 3 failed attempts.
    Enter your password:
    ieatmonkeys
    Wrong Password! System will self destruct after 3 failed attempts.
    Enter your password:
    damniforgotmypass
    BOOOOOOOOOOOOOOOOOOM
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. The Following User Says Thank You to JavaPF For This Useful Post:

    Dave (August 26th, 2009)

  8. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Password screens

    Thank you very much

    i have 1 last question and it concerns the time recording application that was done before (see Help Code Needed thread)

    i am looking to add a method that will warn the user if there has been a sudden change by 5 minutes or above.

    Thanks
    Dave

  9. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Password screens

    Quote Originally Posted by Dave View Post
    Thank you very much

    i have 1 last question and it concerns the time recording application that was done before (see Help Code Needed thread)

    i am looking to add a method that will warn the user if there has been a sudden change by 5 minutes or above.

    Thanks
    Dave
    No worries. If this thread is solved please mark it as solved.

    Please post in your other thread and we will take it from there
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  10. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Password screens

    You might want to look into Console (Java Platform SE 6)) for reading password in console. Reading it as a string is not very secure.

    // Json

Similar Threads

  1. password
    By 5723 in forum Algorithms & Recursion
    Replies: 9
    Last Post: July 9th, 2009, 05:26 AM
  2. [SOLVED] java password
    By pwngrammer in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 15th, 2009, 09:49 AM
  3. 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

Tags for this Thread