I have a small program in Java with GUI, which is generating passwords using Secure Random. I am posting my code here because I want to correct myself in style and, eventually, in efficiency of writing code.
Should I use try catch in, for example, button handling or password generating? I saw some user answers who discouraged using try catch statement. I am reading a lot, and I see that everyone has own opinion.
On Win7 & NetBeans IDE program look like this:
I have 4 files:
Main.java
package passwordgenerator;
import java.awt.EventQueue;
public class Main {
private static void loadGUI() {
EventQueue.invokeLater(() -> {
GUI gui = new GUI();
gui.createAndShowGUI();
gui.setVisible(true);
});
}
public static void main(String[] args) {
loadGUI();
}
}
GUI.java (my functions are on bottom of this file) edited
package passwordgenerator;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
@SuppressWarnings("serial")
public class GUI extends JPanel implements ActionListener {
private static final String UPPERCASE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String LOWERCASE_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
private static final String NUMERIC_CHARACTERS = "1234567890";
private static final String SPECIAL_CHARACTERS = "~!@#$%^&*()_|";
/**
* Launches GUI.
* Creates and configures JFrame, adds panel (represented by this class).
*/
public void createAndShowGUI() {
initComponents();
JFrame frame = new JFrame("password generator @re");
//frame.setMinimumSize(new Dimension(FRAME_MIN_WIDTH, FRAME_MIN_HEIGHT));
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
buttonGroup2 = new javax.swing.ButtonGroup();
buttonGroup3 = new javax.swing.ButtonGroup();
buttonGroup4 = new javax.swing.ButtonGroup();
btnGenerate = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
passwordField = new javax.swing.JTextArea();
useAlphanumeric = new javax.swing.JCheckBox();
useSpecialChars = new javax.swing.JCheckBox();
jLabel1 = new javax.swing.JLabel();
useLowerAlphabet = new javax.swing.JCheckBox();
jSlider1 = new javax.swing.JSlider();
jLabel2 = new javax.swing.JLabel();
numberOfCharacters = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
btnGenerate.setMnemonic('\r');
btnGenerate.setText("generate password");
btnGenerate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGenerateActionPerformed(evt);
}
});
jButton2.setText("copy to clipboard");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
copyToClipboard(evt);
}
});
passwordField.setColumns(20);
passwordField.setRows(5);
jScrollPane1.setViewportView(passwordField);
useAlphanumeric.setSelected(true);
useAlphanumeric.setText("1-9");
useSpecialChars.setSelected(true);
useSpecialChars.setText("special chars (~!@#$...)");
jLabel1.setText("mixing password chars ");
useLowerAlphabet.setSelected(true);
useLowerAlphabet.setText("a-z");
jSlider1.setMajorTickSpacing(3);
jSlider1.setMaximum(30);
jSlider1.setMinimum(5);
jSlider1.setMinorTickSpacing(1);
jSlider1.setPaintLabels(true);
jSlider1.setPaintTicks(true);
jSlider1.setValue(8);
jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
sliderStateChanged(evt);
}
});
jLabel2.setText("number of characters:");
numberOfCharacters.setText("8");
jLabel4.setText("✓ A-Z");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(useSpecialChars)
.addComponent(useAlphanumeric))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(62, 62, 62)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(useLowerAlphabet)
.addComponent(numberOfCharacters, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(117, 117, 117))))))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(jButton2)
.addGap(18, 18, 18)
.addComponent(btnGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(102, 102, 102)
.addComponent(jLabel2)))
.addContainerGap(51, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(91, 91, 91)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(useAlphanumeric)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(useSpecialChars)
.addComponent(useLowerAlphabet))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 3, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(numberOfCharacters)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(15, 15, 15))
);
}// </editor-fold>
private void btnGenerateActionPerformed(java.awt.event.ActionEvent evt) {
String charactersToBeUsed = "";
charactersToBeUsed = UPPERCASE_CHARACTERS; // always
if (useLowerAlphabet.isSelected()) {
charactersToBeUsed += LOWERCASE_CHARACTERS;
}
if (useAlphanumeric.isSelected()) {
charactersToBeUsed += NUMERIC_CHARACTERS;
}
if (useSpecialChars.isSelected()) {
charactersToBeUsed += SPECIAL_CHARACTERS;
}
PasswordGenerator pg = new PasswordGenerator(charactersToBeUsed);
int numberOfChars = Integer.parseInt(numberOfCharacters.getText());
String pw = pg.generate(numberOfChars);
passwordField.setEditable(false);
passwordField.setText(pw);
}
private void sliderStateChanged(javax.swing.event.ChangeEvent evt) {
JSlider source = (JSlider) evt.getSource();
if (!source.getValueIsAdjusting()) {
numberOfCharacters.setText(Integer.toString(source.getValue()));
}
}
private void copyToClipboard(java.awt.event.ActionEvent evt) {
String myString = passwordField.getText();
StringSelection stringSelection = new StringSelection (myString);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard ();
clpbrd.setContents (stringSelection, null);
}
// Variables declaration - do not modify
private javax.swing.JButton btnGenerate;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.ButtonGroup buttonGroup2;
private javax.swing.ButtonGroup buttonGroup3;
private javax.swing.ButtonGroup buttonGroup4;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private static javax.swing.JSlider jSlider1;
private javax.swing.JLabel numberOfCharacters;
public static javax.swing.JTextArea passwordField;
public static javax.swing.JCheckBox useAlphanumeric;
public static javax.swing.JCheckBox useLowerAlphabet;
public static javax.swing.JCheckBox useSpecialChars;
// End of variables declaration
@Override
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
PasswordGenerator.java edited
(Based on this other post)
package passwordgenerator;
import java.security.SecureRandom;
public class PasswordGenerator {
String charactersToBeUsed;
private static final String UPPERCASE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final int DEFAULT_PASSWORD_LENGTH = 8;
// default constructor
public PasswordGenerator() {
this(UPPERCASE_CHARACTERS);
}
// constructor
public PasswordGenerator(String characters) {
this.charactersToBeUsed = characters;
}
public String generate() {
return generate(DEFAULT_PASSWORD_LENGTH);
}
public String generate(int length) {
char[] password = new char[length];
char[] possibleCharacters = charactersToBeUsed.toCharArray();
SecureRandom r = new SecureRandom();
for (int i = 0; i < length; i++) {
password[i] = possibleCharacters[r.nextInt(possibleCharacters.length)];
}
return new String(password);
}
}
EnumAsciiCharacters.java
package passwordgenerator;
public enum EnumAsciiCharacters {
UPPERCASE( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ),
LOWERCASE( "abcdefghijklmnopqrstuvwxyz" ),
NUMERIC( "1234567890" ),
SPECIAL( "~!@#$%^&*()_|" );
private String characters;
@Override
public String toString() {
return this.characters;
}
private EnumAsciiCharacters( String characters ) {
this.characters = characters;
}
} // EnumAsciiCharacters
password[i] += ...
it should bepassword[i] = ...
\$\endgroup\$