I wrote the code for the following 2 combo boxes which display a drop-down list of dorm buildings and meal plans for a college student. The code works, and both boxes are displayed. I have completed what I have to do, but want to find a way to get the total of both categories, and display that value. I am confused as to how to display that value when the array that holds the information is a String.... Is there a way to assign a value to each array item? Here is what I have....
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main extends JFrame
{
private JPanel dormPanel;
private JPanel selectedDormPanel;
private JComboBox dormBox;
private JLabel dormLabel;
private JTextField selectedDorm;
private JPanel mealPanel;
private JPanel selectedMealPanel;
private JComboBox mealBox;
private JLabel mealLabel;
private JTextField selectedMeal;
private String[] dorm = { "Allen Hall: $1500/semester", "Pike Hall: $1600/semester", "Farthing Hall: $1200/semester", "University Suites: $1800/semester"};
private String[] meal = {"7 meals per week: $560/ semester", "14 meals per week: $1,095/ semester", "Unlimited meals: $1,500/ semester"};
public Main()
{
super("Selection");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildDormPanel();
buildSelectedDormPanel();
buildMealPanel();
buildSelectedMealPanel();
add(dormPanel, BorderLayout.CENTER);
add(selectedDormPanel, BorderLayout.SOUTH);
add(mealPanel, BorderLayout.CENTER);
add(selectedMealPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
private void buildDormPanel()
{
dormPanel = new JPanel();
dormBox = new JComboBox(dorm);
dormBox.addActionListener(new DormListener());
dormPanel.add(dormBox);
}
private void buildMealPanel()
{
mealPanel = new JPanel();
mealBox = new JComboBox(meal);
mealBox.addActionListener(new MealListener());
mealPanel.add(mealBox);
}
private void buildSelectedDormPanel()
{
selectedDormPanel = new JPanel();
dormLabel = new JLabel("Dorm: ");
selectedDorm = new JTextField (25);
selectedDorm.setEditable(false);
selectedDormPanel.add(dormLabel);
selectedDormPanel.add(selectedDorm);
}
private void buildSelectedMealPanel()
{
selectedMealPanel = new JPanel();
mealLabel = new JLabel("Meal: ");
selectedMeal = new JTextField (25);
selectedMeal.setEditable(false);
selectedMealPanel.add(mealLabel);
selectedMealPanel.add(selectedMeal);
}
private class DormListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = (String) dormBox.getSelectedItem();
selectedDorm.setText(selection);
}
}
private class MealListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = (String) mealBox.getSelectedItem();
selectedMeal.setText(selection);
}
}
public static void main(String[] args)
{
new Dorm();
new Meal();
}
}