The following code makes the revision helper, seen below:
![[hashmapgui.png|400]]
* Key terms appear in the left hand JTextField and values or definitions appear in the right hand JTextArea. For example, a user could write bit in the JTextField and have binary digit appear in the JTextArea
* Users can add their own key terms and definitions using the Add button. They can lookup a definition using the Lookup button.
* The key terms and definitions are stored as a HashMap, which can be saved and loaded using the appropriate buttons. A JFileChooser is used to select a file name, and then objects are written or read using ObjectOutputStream or ObjectInputStream.
```java
package guihashmap;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class GUIHashMap extends JFrame implements ActionListener {
JButton load, save, add, lookup;
JTextField txtKey;
JTextArea taValue;
JScrollPane scrollPane;
HashMap hashMap = new HashMap();
GUIHashMap()
{
setSize(400, 120);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
load = new JButton("Load");
save = new JButton("Save");
add = new JButton("Add");
lookup = new JButton("Lookup");
txtKey = new JTextField();
taValue = new JTextArea();
taValue.setLineWrap(true);
scrollPane = new JScrollPane(taValue);
setLayout(new GridLayout(3,2));
add(txtKey);
add(taValue);
add(add);
add(lookup);
add(load);
add(save);
add.addActionListener(this);
lookup.addActionListener(this);
load.addActionListener(this);
save.addActionListener(this);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new GUIHashMap();
}
@Override
public void actionPerformed(ActionEvent evt) {
// TODO Auto-generated method stub
if (evt.getSource() == load)
{
loadMap();
}
else if (evt.getSource() == save)
{
saveMap();
}
else if (evt.getSource() == lookup)
{
lookupMap(txtKey.getText());
}
else if (evt.getSource() == add)
{
addMap(txtKey.getText(), taValue.getText());
}
}
private void addMap(String key, String value)
{
hashMap.put(key, value);
}
private void lookupMap(String key)
{
String value = (String)hashMap.get(key);
taValue.setText(value);
}
@SuppressWarnings("unchecked")
private void loadMap()
{
JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
try
{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
hashMap = (HashMap) ois.readObject();
ois.close();
}
catch(IOException ioe)
{
System.err.println(ioe);
} catch (ClassNotFoundException e)
{
System.err.println(e);
}
}
}
private void saveMap()
{
JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooser.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
try
{
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(hashMap);
oos.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
}
```