Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need some help trying to get a comboBox to appear in a JOptionPane after I sel

ID: 3857545 • Letter: I

Question

I need some help trying to get a comboBox to appear in a JOptionPane after I select a previous comboBox selection. I need select "Update" from a comboBox and then click the "Proceed" button, upon hitting the button I would like for a JOptionPane to appear that has selections "A" - "F". My code is below, any help would be greatly appreciated.

<code>

import java.awt.*;

import java.awt.event.*;

import java.util.HashMap;

import javax.swing.*;

public class GUI extends JFrame implements ActionListener{

private HashMap<Integer, Student> map = new HashMap<Integer,Student>();

private JButton processRequest = new JButton("Process Request");

private JComboBox<String> comboBox = new JComboBox<String>();

private JComboBox<String> comboBox2 = new JComboBox<String>();

private JLabel label = new JLabel("Choose a request:");

private JLabel label1 = new JLabel("Id:");

private JLabel label2 = new JLabel("Name:");

private JLabel label3 = new JLabel("Major:");

private JTextField text1 = new JTextField("");

private JTextField text2 = new JTextField("");

private JTextField text3 = new JTextField("");

public GUI(){

setLayout(new GridLayout(0,2));

comboBox.addItem("Insert");

comboBox.addItem("Find");

comboBox.addItem("Update");

comboBox.addItem("Delete");

comboBox2.addItem("A");

comboBox2.addItem("B");

comboBox2.addItem("C");

comboBox2.addItem("D");

comboBox2.addItem("F");

add(label1);

add(text1);

add(label2);

add(text2);

add(label3);

add(text3);

add(label);

add(comboBox);

add(processRequest);

processRequest.addActionListener(this);

}

public void actionPerformed(ActionEvent e){

if(comboBox.getSelectedItem().equals("Insert")){

String str = text1.getText();

int sid = Integer.parseInt(str);

String sname = text2.getText();

String smajor = text3.getText();

text1.setText("");

text2.setText("");

text3.setText("");

Student student = new Student(sname, smajor);

if(map.containsKey(sid))

JOptionPane.showMessageDialog(null, sid + " is already inserted in the database.");

else

map.put(sid, student);

}

else if(comboBox.getSelectedItem().equals("Find")){

String str = text1.getText();

int sid = Integer.parseInt(str);

if(map.containsKey(sid)){

String result = "ID: " + sid + ", " + map.get(sid);

JOptionPane.showMessageDialog(null, result);

}

else

JOptionPane.showMessageDialog(null, sid + " is not found in the database.");

}

else if(comboBox.getSelectedItem().equals("Update")){

String str = JOptionPane.showInputDialog("Enter student ID:");

int sid = Integer.parseInt(str);

str = JOptionPane.showInputDialog("Enter the course grade:");

char grade = Character.toUpperCase(str.charAt(0));

str = JOptionPane.showInputDialog("Enter credit hours:");

double hours = Double.parseDouble(str);

if(map.containsKey(sid)){

map.get(sid).courseCompleted(grade, hours);

}

else

JOptionPane.showMessageDialog(null, sid + " is not found in the database.");

}

else if(comboBox.getSelectedItem().equals("Delete")){

String str = JOptionPane.showInputDialog("Enter student ID:");

int sid = Integer.parseInt(str);

if(map.containsKey(sid))

map.remove(sid);

else

JOptionPane.showMessageDialog(null, sid + "is not found in the database.");

}

else{

System.out.println();

}

}

public static void main(String [] args){

GUI frame = new GUI();

frame.setTitle("Project 4");

frame.setSize(400, 300);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

</code>

Explanation / Answer

import java.awt.*;

import java.awt.event.*;

import java.util.HashMap;

import javax.swing.*;

public class GUI extends JFrame implements ActionListener{

private HashMap<Integer, Student> map = new HashMap<Integer,Student>();

private JButton processRequest = new JButton("Process Request");

private JComboBox<String> comboBox = new JComboBox<String>();

private JComboBox<String> comboBox2 = new JComboBox<String>();

private JLabel label = new JLabel("Choose a request:");

private JLabel label1 = new JLabel("Id:");

private JLabel label2 = new JLabel("Name:");

private JLabel label3 = new JLabel("Major:");

private JTextField text1 = new JTextField("");

private JTextField text2 = new JTextField("");

private JTextField text3 = new JTextField("");

public GUI(){

setLayout(new GridLayout(0,2));

comboBox.addItem("Insert");

comboBox.addItem("Find");

comboBox.addItem("Update");

comboBox.addItem("Delete");

comboBox2.addItem("A");

comboBox2.addItem("B");

comboBox2.addItem("C");

comboBox2.addItem("D");

comboBox2.addItem("F");

add(label1);

add(text1);

add(label2);

add(text2);

add(label3);

add(text3);

add(label);

add(comboBox);

add(processRequest);

processRequest.addActionListener(this);

}

public void actionPerformed(ActionEvent e){

if(comboBox.getSelectedItem().equals("Insert")){

String str = text1.getText();

int sid = Integer.parseInt(str);

String sname = text2.getText();

String smajor = text3.getText();

text1.setText("");

text2.setText("");

text3.setText("");

Student student = new Student(sname, smajor);

if(map.containsKey(sid))

JOptionPane.showMessageDialog(null, sid + " is already inserted in the database.");

else

map.put(sid, student);

}

else if(comboBox.getSelectedItem().equals("Find")){

String str = text1.getText();

int sid = Integer.parseInt(str);

if(map.containsKey(sid)){

String result = "ID: " + sid + ", " + map.get(sid);

JOptionPane.showMessageDialog(null, result);

}

else

JOptionPane.showMessageDialog(null, sid + " is not found in the database.");

}

else if(comboBox.getSelectedItem().equals("Update")){

String str = JOptionPane.showInputDialog("Enter student ID:");

int sid = Integer.parseInt(str);

str = JOptionPane.showInputDialog("Enter the course grade:");

char grade = Character.toUpperCase(str.charAt(0));

str = JOptionPane.showInputDialog("Enter credit hours:");

double hours = Double.parseDouble(str);

if(map.containsKey(sid)){

map.get(sid).courseCompleted(grade, hours);

}

else

JOptionPane.showMessageDialog(null, sid + " is not found in the database.");

}

else if(comboBox.getSelectedItem().equals("Delete")){

String str = JOptionPane.showInputDialog("Enter student ID:");

int sid = Integer.parseInt(str);

if(map.containsKey(sid))

map.remove(sid);

else

JOptionPane.showMessageDialog(null, sid + "is not found in the database.");

}

else{

System.out.println();

}

}

public static void main(String [] args){

GUI frame = new GUI();

frame.setTitle("Project 4");

frame.setSize(400, 300);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}