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
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.
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
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
a console program
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.
Dave (August 26th, 2009)
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.
Dave (August 26th, 2009)
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
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.
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