Combo Box Question...

Reply

Join Date: Sep 2008
Posts: 38
Reputation: cproud21 has a little shameless behaviour in the past 
Solved Threads: 0
cproud21 cproud21 is offline Offline
Light Poster

Combo Box Question...

 
0
  #1
Nov 10th, 2008
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();
    }
}
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,444
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Combo Box Question...

 
0
  #2
Nov 10th, 2008
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,191
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Combo Box Question...

 
-1
  #3
Nov 10th, 2008
@cproud21 why do you have always create new post just continue with same question? Same topic you started here
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC