all. I'm writing a GUI program that allows the user to enter a student id, name, and major. They can either insert these values as a record into the student database, delete them, find them, or update them. What I want to do is create a check- maybe an if statement- to keep the user from inserting an id that's already in the Hashmap.
processButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae){
int idKey = Integer.parseInt(idText.getText());
String nameValue = nameText.getText();
String majorValue = majorText.getText();
String s = (String)comboList.getSelectedItem();
switch(s) {
case "Insert":
hmap.put(idKey, nameValue);
JOptionPane.showMessageDialog(null, "Student added.");
idText.setText("");
nameText.setText("");
majorText.setText("");
break;
case "Delete":
hmap.remove(idKey);
JOptionPane.showMessageDialog(null, "Student removed.");
idText.setText("");
nameText.setText("");
majorText.setText("");
break;
case "Find":
String var = hmap.get(idKey);
JOptionPane.showMessageDialog(null, "Student found."
+ "\n" + var);
idText.setText("");
nameText.setText("");
majorText.setText("");
break;
case "Update":
JFrame frame = new JFrame();
Object[] grades = {"A", "B", "C", "D", "F"};
String gradeAdded = (String)JOptionPane.showInputDialog(frame, "Choose grade:",
"", JOptionPane.QUESTION_MESSAGE, null, grades, grades[0]);
Object[] credits = {"3", "6"};
String creditsAdded = (String)JOptionPane.showInputDialog(frame, "Choose credits:",
"", JOptionPane.QUESTION_MESSAGE, null, credits, credits[0]);
idText.setText("");
nameText.setText("");
majorText.setText("");
break;
}
}
});
This is my code for the processing button actionlistener. In the insert case I would like to create this check. Thanks!
Aucun commentaire:
Enregistrer un commentaire