Results 1 to 11 of 11
  1. #1
    krechlich is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Cool Help with password masking, please

    Okay, so i'm trying to make a login program and i want every character to be masked with '*'. Could someone tell me the code so that every character, which is entered on the password field (passwordTxt) will be masked automatically with '*'? My code is this:

    Java Code:
    package login;
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    import java.awt.*;
    import java.util.Scanner;
    
    public class Main {
    
        private static void createAndShowGUI() {
            final String newLine = System.getProperty("line.separator");
    
    //Create AddressBook
            File AccountBook = new File("AddressBook.txt");
            boolean regExists = false;
            regExists = AccountBook.exists();
            try {
                System.out.println("Creating address book...");
                if (regExists != true) {
                    FileWriter fOut = new FileWriter(AccountBook);
                    BufferedWriter fileOut = new BufferedWriter(fOut);
                    String AccountBookBan = "Accounts" + newLine + newLine;
                    fileOut.write(AccountBookBan);
                    fileOut.close();
                    System.out.println("Address book created!" + newLine);
                } else {
                    System.out.println("Address book was not created, because it already exists!" + newLine);
                }
            } catch (IOException error) {
                System.out.println("Error creating address book!" + newLine);
                JOptionPane.showMessageDialog(null, "Error creating address book!");
            }
    
    
    //Program layout
            final JFrame mainFr = new JFrame();
            mainFr.setTitle("User Login");
            mainFr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            int windowWidth = 380;
            int windowHeight = 200;
            Point centP = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
    
            Container cPane = mainFr.getContentPane();
    
            JButton loginB = new JButton("Login");
            JButton registerB = new JButton("Register");
            JButton cancelB = new JButton("Cancel");
            JPanel buttonPanel = new JPanel();
            FlowLayout bpLayout = new FlowLayout();
            buttonPanel.setLayout(bpLayout);
            bpLayout.setHgap(35);
            buttonPanel.add(loginB);
            buttonPanel.add(registerB);
            buttonPanel.add(cancelB);
            cPane.add(buttonPanel, BorderLayout.PAGE_END);
    
            JLabel banLab = new JLabel("Login or register new account");
            JPanel banPanel = new JPanel();
            banPanel.add(banLab);
            cPane.add(banPanel, BorderLayout.PAGE_START);
    
            JLabel usernameLab = new JLabel("Username:");
            JLabel passwordLab = new JLabel("Password:");
            final JTextField usernameTxt = new JTextField("", 12);
            final JTextField passwordTxt = new JTextField("", 12);
            JPanel fieldPane = new JPanel();
            fieldPane.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 0;
            c.anchor = GridBagConstraints.CENTER;
            c.insets = new Insets(10, 10, 10, 30);
            fieldPane.add(usernameLab, c);
            c.gridx = 1;
            c.gridy = 0;
            c.anchor = GridBagConstraints.CENTER;
            c.insets = new Insets(10, 30, 10, 10);
            fieldPane.add(usernameTxt, c);
            c.gridx = 0;
            c.gridy = 1;
            c.anchor = GridBagConstraints.CENTER;
            c.insets = new Insets(10, 10, 10, 30);
            fieldPane.add(passwordLab, c);
            c.gridx = 1;
            c.gridy = 1;
            c.anchor = GridBagConstraints.CENTER;
            c.insets = new Insets(10, 30, 10, 10);
            fieldPane.add(passwordTxt, c);
            cPane.add(fieldPane, BorderLayout.CENTER);
    
    //Register
            registerB.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Registering...");
    
                    try {
                        String usernameGetText = usernameTxt.getText();
                        String passwordGetText = passwordTxt.getText();
    
                        if (usernameGetText.contains(" ")) {
                            System.out.println("Username cannot contain space!" + newLine);
                            JOptionPane.showMessageDialog(null, "Username cannot contain space!");
                            usernameTxt.setText("");
                            passwordTxt.setText("");
                        } else if (passwordGetText.contains(" ")) {
                            System.out.println("Password cannot contain space!" + newLine);
                            JOptionPane.showMessageDialog(null, "Password cannot contain space!");
                            usernameTxt.setText("");
                            passwordTxt.setText("");
                        } else if (usernameGetText.isEmpty()) {
                            System.out.println("You must fill username field! (Minimum of 4 characters)" + newLine);
                            JOptionPane.showMessageDialog(null, "You must fill username field! (Minimum of 4 characters)");
                            usernameTxt.setText("");
                            passwordTxt.setText("");
                        } else if (passwordGetText.isEmpty()) {
                            System.out.println("You must fill password field! (Minimum of 4 characters)" + newLine);
                            JOptionPane.showMessageDialog(null, "You must fill password field! (Minimum of 4 characters)");
                            usernameTxt.setText("");
                            passwordTxt.setText("");
                        } else if (usernameGetText.length() < 4) {
                            System.out.println("Username must be atleast 4 characters long!" + newLine);
                            JOptionPane.showMessageDialog(null, "Username must be atleast 4 characters long!");
                            usernameTxt.setText("");
                            passwordTxt.setText("");
                        } else if (passwordGetText.length() < 4) {
                            System.out.println("Password must be atleast 4 characters long!" + newLine);
                            JOptionPane.showMessageDialog(null, "Password must be atleast 4 characters long!");
                            usernameTxt.setText("");
                            passwordTxt.setText("");
                        } else if (usernameGetText.equals(passwordGetText)) {
                            System.out.println("Username and password cannot be the same!" + newLine);
                            JOptionPane.showMessageDialog(null, "Username and password cannot be the same!");
                            usernameTxt.setText("");
                            passwordTxt.setText("");
                        } else if (register(usernameGetText)) {
                            System.out.println("Username already exists!" + newLine);
                            JOptionPane.showMessageDialog(null, "Username already exists!");
                            usernameTxt.setText("");
                            passwordTxt.setText("");
                        } else {
                            FileWriter logOut = new FileWriter(new File("AddressBook.txt"), true);
                            BufferedWriter logBuff = new BufferedWriter(logOut);
                            logBuff.write(newLine + "Username: " + usernameGetText + newLine + "Password: " + passwordGetText + newLine + newLine);
                            logBuff.close();
                            System.out.println("Account succesfully created!" + newLine);
                            JOptionPane.showMessageDialog(null, "Account succesfully created!");
                        }
    
                    } catch (IOException error) {
                        System.out.println("Error accessing file!" + newLine);
                        JOptionPane.showMessageDialog(null, "Error accessing file!");
                    }
                }
            });
    
    //Cancel
            cancelB.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Closing...");
                    int response = JOptionPane.showConfirmDialog(null, "Do you want to cancel?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                    if (response == JOptionPane.NO_OPTION) {
                        System.out.println("Prevented!" + newLine);
                    } else if (response == JOptionPane.YES_OPTION) {
                        System.out.println("Closed!" + newLine);
                        System.exit(0);
                    } else if (response == JOptionPane.CLOSED_OPTION) {
                        System.out.println("Prevented!" + newLine);
                    }
                }
            });
    
    //Login
            loginB.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    final String usernameGetText = usernameTxt.getText();
                    final String passwordGetText = passwordTxt.getText();
    
                    System.out.println("Logging in...");
                    if (login(usernameGetText) || login(passwordGetText)) {
                        System.out.println("Username or password doesn't exist!" + newLine);
                        JOptionPane.showMessageDialog(null, "Username or password doesn't exist!");
                    } else {
                        System.out.println("Logged in!" + newLine);
                        JOptionPane.showMessageDialog(null, "Logged in!");
                        System.exit(0);
                    }
                }
            });
    
    
    //Place and size Window
            mainFr.setBounds(centP.x - windowWidth / 2, centP.y - windowHeight / 2, windowWidth, windowHeight);
            mainFr.setVisible(true);
        }
    
    //Read AddressBook for register
        public static boolean register(String inputWord) {
    
            String word = "";
    
            File file = new File("AddressBook.txt");
            final String newLine = System.getProperty("line.separator");
            try {
                Scanner input = new Scanner(file);
                while (input.hasNext()) {
                    word = input.next();
                    if (inputWord.equals(word)) {
                        return true;
                    }
                }
    
            } catch (Exception error) {
                System.out.println("Error accessing file!" + newLine);
                JOptionPane.showMessageDialog(null, "Error accessing file!");
            }
            return false;
        }
    
    //Read AddressBook for login
        public static boolean login(String inputWord) {
    
            String word = "";
    
            File file = new File("AddressBook.txt");
            final String newLine = System.getProperty("line.separator");
            try {
                Scanner input = new Scanner(file);
                while (input.hasNext()) {
                    word = input.next();
                    if (inputWord.equals(word)) {
                        return false;
                    }
                }
    
            } catch (Exception error) {
                System.out.println("Error accessing file!" + newLine);
                JOptionPane.showMessageDialog(null, "Error accessing file!");
            }
            return true;
        }
    
        public static void main(String[] argv) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

  3. #3
    krechlich is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    I would have to change my whole code, and i dont understant anything about that tutorial :D Could you change the code i gave? (If you know how to)

  4. #4
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Just use

    JPasswordField passwordTxt = new JPasswordField(12);

    instead of

    final JTextField passwordTxt = new JTextField("", 12);
    sanjeev,संजीव

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You would learn more by studying that tutorial (in fact a worked example of the task you raise with commented code and links to the API documentation). And being precise about the things you don't understand.

    To be blunt this is a continuation (of both substance and attitude) of the How to find specific text from .txt file thread. As happened in that thread someone may come along and write your code for you, but what is the point of that?

    This is a genuine question. I can see what you miss out on by this approach, but I can't, for the life of me, see what you expect to gain.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    And, no, the code you are being offered is no gain. To be blunt again, the code, with its static methods and use of the deprecated getText() method of JPasswordField, is just cr@ppy.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by krechlich View Post
    I would have to change my whole code
    Not doing so makes legacy code see the light. Managers love your attitude, but managers know zilch about good software. Leave it as it is so you can hack little bits and pieces in it and maybe, just maybe you can get it to work. For fun, at the end, see how much code you have changed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    you can user this also: setEchoChar('*');
    sanjeev,संजीव

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Not doing so makes legacy code see the light. Managers love your attitude, but managers know zilch about good software. Leave it as it is so you can hack little bits and pieces in it and maybe, just maybe you can get it to work. For fun, at the end, see how much code you have changed.

    kind regards,

    Jos
    That ways lies the Big Ball of Mud.

    I worked somewhere that shall remain nameless that had one of them at its heart because they were too afraid to actually rewrite it. Just tinker with it occasionally. Happy days...

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    That ways lies the Big Ball of Mud.

    I worked somewhere that shall remain nameless that had one of them at its heart because they were too afraid to actually rewrite it. Just tinker with it occasionally. Happy days...
    I worked for a small company once that worked with a Big Ball of Mud; they lost the sources (or so they said) and occasionally patched bits of the execuatable; there were even old timers working there who were proud of that patching mess ... I quit after a couple of months ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    krechlich is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by sanjeevtarar View Post
    Just use

    JPasswordField passwordTxt = new JPasswordField(12);

    instead of

    final JTextField passwordTxt = new JTextField("", 12);
    Thanks, got it working with the JPasswordField sanjeevtarar, and thanks to others too for trying to help :)

Similar Threads

  1. Masking a credit card value
    By Samurai Coder in forum New To Java
    Replies: 2
    Last Post: 12-02-2009, 09:05 PM
  2. Masking password for logging
    By firewalll in forum New To Java
    Replies: 1
    Last Post: 09-29-2009, 08:04 AM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 07:53 PM
  4. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 01:22 PM
  5. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 01:11 PM

Posting Permissions

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