| | |
Combo Box Question...
![]() |
•
•
Join Date: Sep 2008
Posts: 38
Reputation:
Solved Threads: 0
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();
}
} 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 (Edit: Example class intentionally left a little bit incomplete.)
java Syntax (Toggle Plain Text)
class DormPlan { private String name; private int cost; public DormPlan(String name, int cost) { this.name = name; this.cost = cost; } @Override public String toString() { return name + ": $" + cost + "/semester"; } public int getCost(){ return cost; } }
Last edited by Ezzaral; Nov 10th, 2008 at 4:48 pm.
@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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- Displaying a list of Databases in a combo box (VB.NET)
- Remove an item from a combo box based on the first letter (VB.NET)
- combo box with related records (MS Access and FileMaker Pro)
- MS Access - Form question (MS Access and FileMaker Pro)
- Combo Box (C#)
- cdo using combo box in my form (ASP)
- Combo Box (C++)
- from database to combo (Visual Basic 4 / 5 / 6)
- combo box (PHP)
- I have a question about functions (Java)
Other Threads in the Java Forum
- Previous Thread: SLF4J
- Next Thread: HELP!!! I cant figure this out!!
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows






