1

I made this to help me practice User Interface. For some reason the password doesn't display on the screen when Generate! is pressed. There are no program errors either. As you can see I have a JLabel for the password.

Code:

package components; 
import java.io.*;
import java.util.Scanner;
import java.util.Random;
import javax.swing.*;
import javax.*;
import java.awt.*;
import java.awt.event.*;

public class PassGenButton extends JPanel implements ActionListener{

protected JButton generate;
protected JLabel passLabel;
public String password = null;

public PassGenButton()
{
    JButton generate = new JButton("Generate!");
    JLabel passLabel = new JLabel(password, JLabel.CENTER);
    passLabel.setFont(new Font("Serif", Font.PLAIN, 36));
    passLabel.setBorder(BorderFactory.createTitledBorder("Password"));
    setLayout(new BorderLayout());
    generate.addActionListener(this);
    add(generate, BorderLayout.SOUTH);
    add(passLabel, BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e)
{
    GetPassword();
}

private static void createAndShowGUI()
{
    JFrame frame = new JFrame("Password Generator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    PassGenButton contentPane = new PassGenButton();

    frame.setContentPane(contentPane);
    frame.setSize(400, 200);
    frame.setLocation(600, 300);
    frame.setVisible(true);
}   

public static void main(String[] args)
{
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            createAndShowGUI();     
        }
    });
}

public void GetPassword()
{
    password = null;
    String[] nouns = new String[2432];
    File file = new File("C:\\Temp\\nounlist.txt");
    String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char[] characters = chars.toCharArray();

    try
    {
        nouns = ReadTextFile(file);
    }
    catch (FileNotFoundException f)
    {
        f.getMessage();
        System.exit(1);
    }

    ShowPassword(nouns, characters);
}

public final String[] ReadTextFile(File aFile) throws FileNotFoundException
{
    String[] strings = new String[2432];
    int counter = 0;
    Scanner scanner = new Scanner(new FileReader(aFile));

    try
    {
        while (scanner.hasNextLine())
        {
            strings[counter] = scanner.nextLine();
            counter++;
        }
    }

    finally
    {
        scanner.close();
    }

    return strings;
}

public void ShowPassword(String[] nouns, char[] characters)
{
    String password;
    Random generator = new Random();
    int chosenNoun = 0;
    int chosenChar = 0;
    int int1 = 0;
    int int2 = 0;

    chosenNoun = generator.nextInt(2432);
    chosenChar = generator.nextInt(26);
    int1 = generator.nextInt(10);
    int2 = generator.nextInt(10);

    password = nouns[chosenNoun] + characters[chosenChar] + Integer.toString(int1) + Integer.toString(int2);
}
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
dsta
  • 253
  • 1
  • 6
  • 15
  • Have a look here. http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string-in-java This might help you. – Elycin Jan 21 '12 at 05:57
  • This is a little bit different because the output is a noun plus a character plus two numbers and it displays it on the JFrame through a JLabel. – dsta Jan 21 '12 at 06:15

4 Answers4

2

Edited snippet

public void GetPassword()
{
    password = null;
    String[] nouns = new String[2432];
    File file = new File("C:\\Temp\\nounlist.txt");
    String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char[] characters = chars.toCharArray();

    try
    {
        nouns = ReadTextFile(file);
    }
    catch (FileNotFoundException f)
    {
        f.printStackTrace();
        //System.exit(1);
    }

    ShowPassword(nouns, characters);
}

Output

java.io.FileNotFoundException: C:\Temp\nounlist.txt (The system cannot find the path specified)
       at java.io.FileInputStream.open(Native Method)
       at java.io.FileInputStream.<init>(FileInputStream.java:120)
       at java.io.FileReader.<init>(FileReader.java:55)
       at PassGenButton.ReadTextFile(PassGenButton.java:78)
       at PassGenButton.GetPassword(PassGenButton.java:63)
       at PassGenButton.actionPerformed(PassGenButton.java:29)
       ...

The output might not be exactly the same where you are, but change the method as specified and copy/paste the output.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

Your passLabel label just sits there. Its value never changes. Your showPassword() method presumably is supposed to display the given password, but it doesn't. It simply creates a string with the password's value, then ends, without ever even looking at the label.

You need a last line reading something like this:

passLabel.setText(password);
2

You're hiding your passLabel variable (as well as your generate button). You've already declared them as instance variables, you don't need to re-declare them in your constructor, just assign values to them. So instead of:

JButton generate = new JButton("Generate!");
JLabel passLabel = new JLabel(password, JLabel.CENTER);

you need to have:

generate = new JButton("Generate!");
passLabel = new JLabel(password, JLabel.CENTER);

Then you need to make sure you set its text as per bdares' answer.

Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72
  • The local variables are indeed hiding the member variables. But that's not called "overloading". Overloading means: having multiple methods with the same name (but different arguments). Variables cannot be overloaded. – Jesper Jan 21 '12 at 07:39
  • @Jasper: oops, true, corrected. That's what I get for looking at a question I answered yesterday about overloading ;-) – Amos M. Carpenter Jan 21 '12 at 07:43
  • Thank you all for helping. I actually changed the Label to a TextField and now it works great. I just have the password writing to the TextField when it generates. – dsta Jan 22 '12 at 00:30
0

you can use ostermillers password generator. He also have examples on his website and a javaapplet http://ostermiller.org/utils/src/RandPass.java.html

Koray Güclü
  • 2,857
  • 1
  • 34
  • 30