I have my program working but i need to replace the JOptionPane.showMessageDialog(null, "The cost is " + totalCost); and just have the totalCost output to the totalCharges textfield.

import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;

  public class DormAndMealPlanCalculator extends JFrame 
     private JPanel dormPanel, mealPanel, buttonPanel, totalChargesPanel;
     private JComboBox dormPanelCom, mealPanelCom;
     private JButton calcButton;
     private JTextField totalCharges;
	private JLabel label1;
  //Constants for the price of each dorm.
     public final int ALLEN_HALL = 1500;
     public final int PIKE_HALL = 1600;
     public final int FARTHING_HALL = 1200;
     public final int UNIVERSITY_SUITES = 1800;
  //Constants for meal costs.
     public final int SEVEN_MEAL = 560;
     public final int FOURTEEN_MEAL = 1095;
     public final int UNLIMITED_MEAL = 1500;
     private int dormCost = 0;
     private int mealCost = 0;
     private int totalCost = 0;
  //String for dormChoice combobox.
     private String[]dormChoice = 
						{ "Allen Hall $ 1,750 per semester", "Pike Hall: $1,600 per semester", 
						  "Farthing Hall: $1,200 per semester", "University Suites: $1,800 per semester" };
     private String[]mealChoice = 
						{ "7 meals per week $ 650 per semester", "14 meals per week: $1,095 per semester",
						  "Unlimited meals: $1,500 per semester" };
     public DormAndMealPlanCalculator()
     //Displays the Name
        super("Dorm Room/Meal Calculator");
     //Set action when closing
     //Create border layout
        setLayout(new BorderLayout());
        add(dormPanel, BorderLayout.WEST);
        add(mealPanel, BorderLayout.EAST);
		add(totalChargesPanel, BorderLayout.SOUTH);
        add(buttonPanel, BorderLayout.NORTH);

     private void buildDormPanel()
        dormPanel = new JPanel();
        dormPanelCom = new JComboBox(dormChoice);
        dormPanelCom.addActionListener(new ComboBoxListener());
     private void buildMealPanel()
        mealPanel = new JPanel();
        mealPanelCom = new JComboBox(mealChoice);
        mealPanelCom.addActionListener(new MealBoxListener());
	// The buttonPanel method builds the bottun panel.
     private void buildButtonPanel()
        buttonPanel = new JPanel();
		calcButton = new JButton("Calculate");
	// The buildSelectedDormPanel builds the selected totalCharges panel.
     private void buildSelectedTotalChargesPanel()
     // Create the totalChargesPanel for the label.      
        totalChargesPanel = new JPanel();
        label1 = new JLabel("Total charges per semester: ");
     // Create the totalCharges textfield.        
        totalCharges = new JTextField (15);
     // Add the totalChargesPanel to the panel.      

     private class ComboBoxListener implements ActionListener
        public void actionPerformed(ActionEvent e)
           String selection = (String) dormPanelCom.getSelectedItem();
           if (selection.equals("Allen Hall $ 1,750 per semester"))
              dormCost = ALLEN_HALL;
           else if (selection.equals("Pike Hall: $1,600 per semester"))
              dormCost = PIKE_HALL;
           else if (selection.equals("Farthing Hall: $1,200 per semester"))
              dormCost = FARTHING_HALL;
           else if (selection.equals("University Suites: $1,800 per semester"))
              dormCost = UNIVERSITY_SUITES;
     private class MealBoxListener implements ActionListener
        public void actionPerformed(ActionEvent e)
           String selection = (String) mealPanelCom.getSelectedItem();
           if (selection.equals("7 meals per week $ 650 per semester"))
              mealCost = SEVEN_MEAL;
           else if (selection.equals("14 meals per week: $1,095 per semester"))
              mealCost = FOURTEEN_MEAL;
           else if (selection.equals("Unlimited meals: $1,500 per semester"))
              mealCost = UNLIMITED_MEAL;
           totalCost = dormCost + mealCost;
           JOptionPane.showMessageDialog(null, "The cost is " + 
     public static void main(String[] arguments){
        DormAndMealPlanCalculator dam = new DormAndMealPlanCalculator();

if I understand correctly this is what you are trying to do.


I replace the JOption with totalCharges.setText(totalCost)and I get the error:

DormAndMealPlanCalculator.java:127: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (int)

oh sorry. the error is saying that setText only accepts a string and totalCost is an int. the easiest way to convert an int to an empty string is concatenation with a string


Edited 6 Years Ago by Gerbiler: n/a

Great Thanks......Your Good!!!!!!!!!!!
This article has been dead for over six months. Start a new discussion instead.