patternjavaMinor
Password generator with Secure Random
Viewed 0 times
randomsecurewithgeneratorpassword
Problem
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
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.setDefaultClo
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.setDefaultClo
Solution
Should i use try catch in, for example, button handling or password generating?
try-catch is for handling exceptions. For example in the middle of downloading a URL the network connection can get disrupted and trigger an exception, which can be handled gracefully by retrying to download.
In your app, I don't see what can go wrong,
I don't see what kind of exceptions might be triggered.
"Should I use try catch" is a very strange question to ask.
Using try catch is typically not an option, but mandatory when it's needed.
It's not something you ask yourself.
I saw some user answers who discouraged using try catch statement. I am reading a lot, and I see that everyone has own opinion.
I suspect there is a bigger context there when somebody discourages using try-catch.
It doesn't make sense to say so, the statement has no meaning,
without explaining the context where it would be true.
Using try-catch is not a matter of opinion,
the operations you're doing either force you to handle exceptions or don't,
not your choice.
If i write another program, all by myself or in bigger part myself, can i post code here, even if we have here a lot of similar projects? Like my passwordGenerator - there are a lot of similar threads.
It's normal to have similar threads.
Code you wrote yourself, to the best of your knowledge,
fully working,
and you would like it peer-reviewed,
then this is a good place to post it.
Code review
The way you create the layout,
with the deeply chained
is a bit hard to read,
but I don't know how to do that better.
Maybe somebody else can advise you on that.
In the GUI class,
many variables that are declared members should be local variables instead:
It's good to limit the scope of variables to the minimum.
The reason is simple:
when a variable is not accessible in a bigger scope than necessary,
it cannot be misused.
There is
so that cannot be converted to a local variable.
However, if it's so different from the others,
then it deserves a better name than "jLabel3",
as it's clearly a bit special.
And why do you refer to classes with the complete package names instead of importing them? This is very tedious.
Judging by
it looks like you're using Java 8.
In which case you can simplify some anonymous classes,
for example instead of:
You can write:
There are some other places where you could apply the sample logic, for example:
try-catch is for handling exceptions. For example in the middle of downloading a URL the network connection can get disrupted and trigger an exception, which can be handled gracefully by retrying to download.
In your app, I don't see what can go wrong,
I don't see what kind of exceptions might be triggered.
"Should I use try catch" is a very strange question to ask.
Using try catch is typically not an option, but mandatory when it's needed.
It's not something you ask yourself.
I saw some user answers who discouraged using try catch statement. I am reading a lot, and I see that everyone has own opinion.
I suspect there is a bigger context there when somebody discourages using try-catch.
It doesn't make sense to say so, the statement has no meaning,
without explaining the context where it would be true.
Using try-catch is not a matter of opinion,
the operations you're doing either force you to handle exceptions or don't,
not your choice.
If i write another program, all by myself or in bigger part myself, can i post code here, even if we have here a lot of similar projects? Like my passwordGenerator - there are a lot of similar threads.
It's normal to have similar threads.
Code you wrote yourself, to the best of your knowledge,
fully working,
and you would like it peer-reviewed,
then this is a good place to post it.
Code review
The way you create the layout,
with the deeply chained
.addGroup, add-this, add-that,is a bit hard to read,
but I don't know how to do that better.
Maybe somebody else can advise you on that.
In the GUI class,
many variables that are declared members should be local variables instead:
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;It's good to limit the scope of variables to the minimum.
The reason is simple:
when a variable is not accessible in a bigger scope than necessary,
it cannot be misused.
There is
jLabel3 which is used by more than one method,so that cannot be converted to a local variable.
However, if it's so different from the others,
then it deserves a better name than "jLabel3",
as it's clearly a bit special.
And why do you refer to classes with the complete package names instead of importing them? This is very tedious.
Judging by
loadGUI,it looks like you're using Java 8.
In which case you can simplify some anonymous classes,
for example instead of:
btnGenerate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGenerateActionPerformed(evt);
}
});You can write:
btnGenerate.addActionListener(this::btnGenerateActionPerformed);There are some other places where you could apply the sample logic, for example:
jButton2.addActionListener(GUI.this::copyToClipboard);Code Snippets
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;btnGenerate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGenerateActionPerformed(evt);
}
});btnGenerate.addActionListener(this::btnGenerateActionPerformed);jButton2.addActionListener(GUI.this::copyToClipboard);Context
StackExchange Code Review Q#92902, answer score: 4
Revisions (0)
No revisions yet.