Jump to content

Random Password Generator


BrainDeadGenius

Recommended Posts

First "script" done in Java.

I wasn't able to do everything how I wanted to, and I'm sure there's a ton of ways I could upgrade the limited features I did do. This was kind of just trying to get used to Java and how to operate with features I'm not used to from PHP.

 

The password generator allows you to choose the characters you want (lowercase alphabet, uppercase alphabet, symbols, and numbers) and you can set the length you wish for the password to be. From there, you're able to save the password with an identifying. With what I create, the only method I got to work how I wanted to was to create a text file with the content (so the script can always see the content). Each time the script runs, or a new password is saved, the saved passwords section updates.

 

https://github.com/engineertdog/JavaPasswordGenerator

There's the .java and .form source files available.

 

Please criticize the content as need be.

Edited by BrainDeadGenius
Link to comment
Share on other sites

You asked me to take a look at your code, which I gave a small review here. I got on a computer and was able to run this, got an error right away:

java.io.FileNotFoundException: gLEPkie8@4MCmCD.txt (The system cannot find the file specified)

This is because you never check to see if the file has already been created; you assume the file already exists on my system, which it doesn't. You should be doing:

File file = new File("gLEPkie8@4MCmCD.txt");
if(!file.exists()) 
    file.createFile();

You aren't setting the location of your frame. Usually, people center it, although even that can cause problems (people not realizing they accidentally opened multiple windows). Try implementing JFrame#setLocationByPlatform(boolean) and tell me how you like it.

 

I see you attempted to add a hints to your text field (shadowed text). The problem is, it doesn't have that "shadow" feel, and doesn't disappear when you click on the field. Here's a little class I built up, which supports hints:

import java.awt.Color;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JTextField;

public class JHintedTextField extends JTextField {
	private String hintedText;
	private boolean hintDisplayed;
	
	public JHintedTextField(int columns, String hintedText) {
		super(columns);

		this.hintedText = hintedText == null ? "" : hintedText;
		addFocusListener(adapter);
		displayHint(hintedText);
	}
	
	public JHintedTextField(String hintedText) {
		this(0, hintedText);
	}
	

	private FocusAdapter adapter = new FocusAdapter() {
		public void focusGained(FocusEvent e) {
			if(hintDisplayed)
				removeHint();
		}
		
		public void focusLost(FocusEvent e) {
			if(!hintDisplayed && getText().isEmpty())
				displayHint(hintedText);
		}
	};
	
	private void displayHint(String hint) {
		setText(hint);
		setForeground(Color.GRAY);
		hintDisplayed = true;
	}
	
	private void removeHint() {
		setText("");
		setForeground(Color.BLACK);
		hintDisplayed = false;
	}
}

Rather than using whats referred to as a stream loop [while((line = in.readLine()) != null)] is redundant when using a BufferedReader ever since Java 8:

BufferedReader reader = ...;
reader.lines().forEach(line -> {
    ...
});

I can add the same user/pass to the "Saved Passwords". You should instead be using some kind of Set to store your entries. It would also be better practice to combine the label and field into it's own panel (a new panel per entry), so you could add functionality such as right clicking an entry to remove it.

 

As for the problem you told me earlier (preserving the state of a map between shutdowns of your app), here's a little example of storing a Map object onto your drive, then loading it back into memory:

void saveObject(Map<String, String> map) throws IOException {
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("name.anyAbbreviation"));
    out.writeObject(map);
}

Map<String, String> loadObject() throws IOException, ClassNotFoundException {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("name.anyAbbreviation"));
        
    Map<String, String> m = (Map<String, String>) in.readObject();
    return m;
}

Rather than using a map, though, you should create a new type, PasswordEntry, which contains the information for the entry (such as the name and password as you have now, but also things such as suggested expiration dates, date created, and possibly details about the account it's saved for such as "This is for a woodcutting account" or something").

 

I have to hop off the computer, but hopefully what I've said so far helped. I'm sure there's more that can be fixed up, so let me know if you want me to take another look once you've fixed it up a bit.

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...