943,846 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 651
  • Java RSS
Nov 10th, 2008
0

Combo Box Question...

Expand Post »
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();
    }
}
Similar Threads
Reputation Points: 4
Solved Threads: 0
Light Poster
cproud21 is offline Offline
42 posts
since Sep 2008
Nov 10th, 2008
0

Re: Combo Box Question...

Yes, that's what objects are for and you can place any object type you want into a combo box. The toString() value of the object will be displayed as the text if the object is not itself a String. So consider how a small class like this could be used
java Syntax (Toggle Plain Text)
  1. class DormPlan {
  2.  
  3. private String name;
  4. private int cost;
  5.  
  6. public DormPlan(String name, int cost) {
  7. this.name = name;
  8. this.cost = cost;
  9. }
  10.  
  11. @Override
  12. public String toString() {
  13. return name + ": $" + cost + "/semester";
  14. }
  15.  
  16. public int getCost(){
  17. return cost;
  18. }
  19. }
(Edit: Example class intentionally left a little bit incomplete.)
Last edited by Ezzaral; Nov 10th, 2008 at 4:48 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 10th, 2008
-1

Re: Combo Box Question...

@cproud21 why do you have always create new post just continue with same question? Same topic you started here
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: SLF4J
Next Thread in Java Forum Timeline: HELP!!! I cant figure this out!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC