I've been working on my program for a while and I can't seem to figure it out, since I still consider myself new to the language. Anyways, my professor is looking for the GUI Application to calculate charges when selecting dorms and meals, and when you click the "Calculate" button, you're suppose to get the total charges. Only in my program, when I select either a dorm or meal it calculates the total charges without clicking the button. Can anyone help me out? I'd very much appreciate it. I will also attach a pic on what the expected output my professor is looking for. Thanks.
import java.awt.*; // Import common GUI elements. import java.awt.event.*; // Import common GUI event listeners. import javax.swing.*; // Import more common GUI elements. /** DormAndMealPlanCalculator class. */ public class DormAndMealPlanCalculator extends JFrame { private static final long serialVersionUID = 1L; private JLabel label; // Display a message. private JPanel dormPanel; // To hold dorm panel components. private JPanel selectedDormPanel; // To hold selected dorm panel components. private JComboBox dormBox; // List of dorms. private JTextField selectedDorm; // Selected dorm. private JPanel mealPanel; // To hold meal panel components. private JPanel selectedMealPanel; // To hold selected meal panel components. private JComboBox mealBox; // List of meal plans. private JTextField selectedMeal; // Selected meal plan. private JButton calcButton; // To hold total panel component. private JTextField total; // Total plan. /** Array to hold the values of the dormitory combo box. */ private String[] dorm = { "Allen Hall $" + 1500, "Pike Hall $" + 1600, "Farthing Hall $" + 1200, "University Suites $" + 1800 }; /** Array to hold the dormitory rate. */ double[] dRate = {1500, 1600, 1200, 1800}; /** Array to hold the values of the meal combo box. */ private String[] meal = { "7 Meals Per Week $" + 560, "14 Meals Per Week $" + 1095, "Unlimited Meals $" + 1500 }; /** Array to hold the meal plan rate. */ double[] mRate = {560, 1095, 1500}; /** Constructor. */ public DormAndMealPlanCalculator() { setTitle("Dorm and Meal Plan Calculator."); // Set the title. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Specify action for close button. setLayout(new GridLayout(3,2)); // Create a border layout manager. // Build panels. buildDormPanel(); buildMealPanel(); buildTotalPanel(); // Add panels to content pane. add(dormPanel); add(mealPanel); add(calcButton); // Pack and display. pack(); setVisible(true); } /** buildDormPanel function that adds a combo box with the dormitories to the panel. */ private void buildDormPanel() { dormPanel = new JPanel(); // Create panel to hold combo box. dormBox = new JComboBox(dorm); // Create the combo box. // Register an action listener. dormBox.addActionListener(new ComboBoxListener()); dormPanel.add(dormBox); } /** buildMealPanel function that adds a combo box with the types of meals to the panel. */ private void buildMealPanel() { mealPanel = new JPanel(); // Create panel to hold combo box. mealBox = new JComboBox(meal); // Create the combo box. // Register an action listener. mealBox.addActionListener(new ComboBoxListener()); mealPanel.add(mealBox); } /** buildSelectedDormPanel function adds a read-only text field to the panel. */ public void buildSelectedDormPanel() { selectedDormPanel = new JPanel(); // Create panel to hold components. label = new JLabel("Your Dormitory Is: "); // Create the label. // Create the uneditable text field. selectedDorm = new JTextField(20); selectedDorm.setEditable(false); // Add the label and text field // to the panel. selectedDormPanel.add(label); selectedDormPanel.add(selectedDorm); } /** buildSelectedMealPanel function adds a read-only text field to the panel. */ public void buildSelectedMealPanel() { selectedMealPanel = new JPanel(); // Create panel to hold components. label = new JLabel("Your Meal Plan Is: "); // Create the label. // Create the uneditable text field. selectedMeal = new JTextField(20); selectedMeal.setEditable(false); // Add the label and text field // to the panel. selectedMealPanel.add(label); selectedMealPanel.add(selectedMeal); } /** buildTotalPanel function adds a read-only text field to the panel. */ private void buildTotalPanel() { calcButton = new JButton("Calculate"); // Create the uneditable text field. total = new JTextField(15); total.setEditable(false); } /** Private inner class that handles the event when the user selects an item from the combo box. */ private class ComboBoxListener implements ActionListener { public void actionPerformed(ActionEvent e) { // Declare variables. int dorm; int meal; double total1; // String total. // Get the selected dormitory. dormBox.getSelectedItem(); dorm = dormBox.getSelectedIndex(); // Get the selected meal plan. mealBox.getSelectedItem(); meal = mealBox.getSelectedIndex(); // Add the selections. total1 = dRate[dorm] + mRate[meal]; total.setText("$" + total1); JOptionPane.showMessageDialog(null, "Total Charges Per Semester: " + total1); } } /** Main function. */ public static void main(String[] args) { new DormAndMealPlanCalculator(); } }