Skip to content

Instantly share code, notes, and snippets.

@smartass08
Last active February 1, 2021 21:34
Show Gist options
  • Save smartass08/7bf473edeb4a0c2d9fdd324b610e459f to your computer and use it in GitHub Desktop.
Save smartass08/7bf473edeb4a0c2d9fdd324b610e459f to your computer and use it in GitHub Desktop.
import javax.swing.*;
import javax.swing.UIManager;
import javax.swing.border.*;
import java.awt.Font;
import java.security.SecureRandom;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.datatransfer.*;
import java.awt.Toolkit;
import java.awt.Color;
public class PasswordGenerator {
public static void main(String[] args) {
Window w = new Window();
}
}
class Window extends JFrame { // Extend Windows from Jframe
private int length;
private boolean nrIsSet = false;
private boolean dark = false;
//private int[] bgcolour = {255,255,255}; //Dark mode : TO DO
//private int[] kek = {255,255,255};
private int input = 1;
Window() {
//defining basic parameters of our Window
UIManager UI = new UIManager(); // Dark mode
setLayout(null);
setSize(500, 300);
setTitle("Password Generator");
setVisible(true);
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
Color font_white = new java.awt.Color(84, 84, 84);
Color co_dark = new java.awt.Color(49, 49, 49);
Color font_dark = new java.awt.Color(148, 148, 148);
// Create JTextField component where we want to input number of characters and read output (password
JTextField textField = new JTextField("Enter length of password");
textField.addMouseListener(new MouseAdapter() {// This Listener makes the initiate text dissapear on click
@Override
public void mouseClicked(MouseEvent e) {
nrIsSet = false;
if (!nrIsSet) {
textField.setText("");
}
}
});
//Setting parameters of our Password Field
Font font1 = new Font("Monoid", Font.BOLD, 13);
textField.setForeground(font_white);
textField.setCaretColor(font_white);
textField.setFont(font1);
textField.setSize(475, 50);
textField.setLocation(10, 50);
JTextField tf = new JTextField();
textField.setVisible(true);
textField.setHorizontalAlignment(JTextField.CENTER);
//Creating generate button
JButton generateButton = new JButton("Generate");
generateButton.setLocation(145, 120);
generateButton.setSize(100, 38);
generateButton.setVisible(true);
generateButton.setForeground(font_white);
//ActionListener starts listening to activites
generateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(isNumeric(textField.getText()));
//Force the necessity of another click for another input for password
if (!nrIsSet) {
//Check if the input we recieved is valid int or not
//If not valid then give a hint about the same
if (isNumeric(textField.getText())) {
length = Integer.parseInt(textField.getText());
nrIsSet = true;
} else {
textField.setText("Invalid input, Provide the length of password in digits.");
}
}
//Finally a basic check if input isn't null
if (length != 0) {
String pswd = new String(genPass(length));
textField.setText(pswd);
}
}
});
//Copy button which makes it easy to copy the password using only one button.
JButton copyToClipboardButton = new JButton("Copy");
copyToClipboardButton.setSize(100, 38);
copyToClipboardButton.setLocation(255, 120);
copyToClipboardButton.setVisible(true);
copyToClipboardButton.setForeground(font_white);
copyToClipboardButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//check if the user didn't change the input again for new password.
if (textField.getText().length() == length) {
StringSelection stringSelection = new StringSelection(textField.getText());
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, null);
}
}
});
JToggleButton DMButton = new JToggleButton("Dark mode");
DMButton.setSize(90, 38);
DMButton.setLocation(390, 220);
DMButton.setVisible(true);
DMButton.setForeground(font_white);
DMButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object[] options = {"Yes",
"No"};
if (!dark) {
UI.put("OptionPane.messageForeground", font_white);
UI.put("OptionPane.background", new java.awt.Color(255, 255, 255));
UI.put("Panel.background", new java.awt.Color(255, 255, 255));
input = JOptionPane.showOptionDialog(null,
"Would you like to enable dark mode?",
"Dark mode",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, // font
options, //the titles of buttons
null); //default button title
} else {
UI.put("OptionPane.background", new java.awt.Color(29, 29, 29));
UI.put("Panel.background", new java.awt.Color(29, 29, 29));
UI.put("OptionPane.messageForeground", font_dark);
input = JOptionPane.showOptionDialog(null,
"Would you like to disable dark mode?",
"Dark mode",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //do not use a custom Icon
options, //the titles of buttons
null); //default button title
}
if (input == 0) {
if (!dark) {
getContentPane().setBackground(new java.awt.Color(29, 29, 29));
textField.setBackground(co_dark);
generateButton.setBackground(co_dark);
copyToClipboardButton.setBackground(co_dark);
DMButton.setBackground(co_dark);
//Enable dark mode
generateButton.setOpaque(true);
copyToClipboardButton.setOpaque(true);
DMButton.setOpaque(true);
//Borders
Border kek = BorderFactory.createEtchedBorder(new java.awt.Color(69, 69, 69), new java.awt.Color(59, 59, 59));
textField.setBorder(kek);
generateButton.setBorder(kek);
copyToClipboardButton.setBorder(kek);
DMButton.setBorder(kek);
//Fonts
textField.setForeground(font_dark);
generateButton.setForeground(font_dark);
copyToClipboardButton.setForeground(font_dark);
DMButton.setForeground(font_dark);
textField.setCaretColor(font_dark);
dark = true;
} else {
//restore Background
getContentPane().setBackground(new java.awt.Color(255, 255, 255));
textField.setBackground(new java.awt.Color(255, 255, 255));
generateButton.setBackground(new JButton().getBackground());
copyToClipboardButton.setBackground(new JButton().getBackground());
DMButton.setBackground(new JButton().getBackground());
//restore Borders
textField.setBorder(new JTextField().getBorder());
generateButton.setBorder(new JButton().getBorder());
copyToClipboardButton.setBorder(new JButton().getBorder());
DMButton.setBorder(new JButton().getBorder());
//restore fonts
textField.setForeground(font_white);
generateButton.setForeground(font_white);
copyToClipboardButton.setForeground(font_white);
DMButton.setForeground(font_white);
textField.setCaretColor(font_white);
dark = false;
}
}
}
});
//Add all the methods & components to Jframe using add()
add(textField);
add(generateButton);
add(copyToClipboardButton);
add(DMButton);
//Refresh the ui
repaint();
}
//Basic function which checks the string's content is numeric or not.
public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
double d = Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
//This is a function which uses import java.security's SecureRandom library to create *TRUE* random password and not useless psuedoradom passwords using norm loops.
// Usage is self explanatory.
public static String genPass(int len) {
final String chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpRrSsTtUuVvWwXxYyZz0123456789!@#$%^&*_=+-/€.?<>)";
SecureRandom random = new SecureRandom(); // This class provides a cryptographically strong random number generator.
StringBuilder sb = new StringBuilder(); // Build a changeable string.
for (int i = 0; i < len; i++) {
int randomIndex = random.nextInt(chars.length());
sb.append(chars.charAt(randomIndex));
}
return sb.toString(); // Make sure to return string and not the whole object
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment