Problem
I made a simple password vault in Java and I was wondering if anyone had any input on it. The code works fine, I just wanted to know if there are any improvements that could be made.
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
public class PasswordVault implements ActionListener
{
static String masterPassword, enteredPassword;
JFrame masterPasswordFrame, passwordVault;
JPasswordField masterPasswordField;
JTable passwordTable;
Object[] columnNames = {"Name of Application", "Application Password", "Description"};
Object[] rowData = new Object[3];
JTextField appName, appPass, appDesc;
JButton add, delete, update;
JLabel nameOfApp, passOfApp, descOfApp;
public static void main(String[] args)
{
String name = JOptionPane.showInputDialog("What is your name?");
masterPassword = JOptionPane.showInputDialog("Hello, " + name + ". " + "What would you like your master password to be?");
new PasswordVault();
}
public PasswordVault()
{
masterPasswordFrame = new JFrame("Master Password");
masterPasswordFrame.setSize(400,100);
masterPasswordFrame.setLocationRelativeTo(null);
masterPasswordFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel masterPasswordPanel = new JPanel();
masterPasswordField = new JPasswordField(10);
masterPasswordField.setEchoChar('*');
JLabel passwordLabel = new JLabel("Enter Password: ");
JButton okayButton = new JButton("Check");
okayButton.addActionListener(this);
masterPasswordPanel.add(passwordLabel);
masterPasswordPanel.add(masterPasswordField);
masterPasswordPanel.add(okayButton);
masterPasswordFrame.add(masterPasswordPanel);
masterPasswordFrame.setVisible(true);
masterPasswordFrame.pack();
enteredPassword = new String(masterPasswordField.getPassword());
passwordVault = new JFrame("Password Vault");
passwordTable = new JTable();
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.setColumnIdentifiers(columnNames);
passwordTable.setModel(tableModel);
appName = new JTextField();
appPass = new JTextField();
appDesc = new JTextField();
add = new JButton("Add");
delete = new JButton("Delete");
update = new JButton("Update");
nameOfApp = new JLabel("App Name: ");
passOfApp = new JLabel("App password: ");
descOfApp = new JLabel("Description: ");
appName.setBounds(400, 220, 100, 25);
appPass.setBounds(400, 250, 100, 25);
appDesc.setBounds(400, 280, 100, 25);
add.setBounds(530, 220, 100, 25);
update.setBounds(530, 250, 100, 25);
delete.setBounds(530, 280, 100, 25);
JScrollPane scrollPane = new JScrollPane(passwordTable);
scrollPane.setBounds(0, 0, 1000, 200);
passwordVault.setLayout(null);
passwordVault.add(scrollPane);
passwordVault.add(nameOfApp);
passwordVault.add(passOfApp);
passwordVault.add(descOfApp);
passwordVault.add(appName);
passwordVault.add(appPass);
passwordVault.add(appDesc);
passwordVault.add(add);
passwordVault.add(update);
passwordVault.add(delete);
add.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
rowData[0] = appName.getText();
rowData[1] = appPass.getText();
rowData[2] = appDesc.getText();
tableModel.addRow(rowData);
appName.setText("");
appPass.setText("");
appDesc.setText("");
}
});
update.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int i = passwordTable.getSelectedRow();
if(i >= 0)
{
tableModel.setValueAt(appName.getText(), i, 0);
tableModel.setValueAt(appPass.getText(), i, 1);
tableModel.setValueAt(appDesc.getText(), i, 2);
appName.setText("");
appPass.setText("");
appDesc.setText("");
}
else
{
JOptionPane.showMessageDialog(null, "Update Error");
}
}
});
delete.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int i = passwordTable.getSelectedRow();
if(i >= 0)
{
tableModel.removeRow(i);
appName.setText("");
appPass.setText("");
appDesc.setText("");
}
else
{
JOptionPane.showMessageDialog(null, "Delete Error");
}
}
});
passwordTable.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
int i = passwordTable.getSelectedRow();
appName.setText(tableModel.getValueAt(i, 0).toString());
appPass.setText(tableModel.getValueAt(i, 1).toString());
appDesc.setText(tableModel.getValueAt(i, 2).toString());
}
});
passwordVault.setSize(1000,500);
passwordVault.setLocationRelativeTo(null);
passwordVault.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event)
{
if(new String(masterPasswordField.getPassword()).equals(masterPassword))
{
masterPasswordFrame.setVisible(false);
passwordVault.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null, "Password Incorrect. Please Try Again.");
}
}
}
Solution
Some things that caught my eye:
you don’t seem to have any persistent storage just in memory. I believe you would get more versatility by implementing external persistent storage. The storage can be in several different formats, physical file on disk, a database, cloud storage.
In line with that, it is usually not a very good idea to store passwords as literal strings. This leaves you prone to hacking. I would suggest looking at the Cryptography api.
Separate the vault from the GUI. What happens if you decide to run the vault from a server in a webpage or as a console application? You basically have to start over again.