Link to home
Start Free TrialLog in
Avatar of b_acs
b_acsFlag for United States of America

asked on

How do I change the layout of my combo boxes?

I have completed my assignment, but I am not satisfied with the final layout.  I would like to move my total box to the right side below my selected boxes.  Also, as of right now, if the user wants to select the "default" then they would have to change one of the selections and then change it back.  Is there a way to "fix" these things?
Avatar of b_acs
b_acs
Flag of United States of America image

ASKER

Whoops forgot to post the code.....
/**
   Chapter 12
   Programming Challenge 3, Dorm and Meal Plan Calculator  
   Author: Brian Acs
   Date:   5/10/2009
 
A university has the following dormitories: 
 
Allen Hall: $1,500 per semester 
Pike Hall: $1,600 per semester 
Farthing Hall: $1,200 per semester 
University Suites: $1,800 per semester 
 
The university also offers the following meal plans: 
7 meals per week: $560 per semester 
14 meals per week: $1,095 per semester 
Unlimited meals: $1,500 per semester 
 
Create an application with two combo boxes. One should
hold the names of the dormitories, and the other should
hold the meal plans. The user should select a dormitory
and a meal plan, and the application should show the total
charges for the semester. 
*/
package dormplanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
/**
This class demonstrates a combo box.
*/
 
@SuppressWarnings("serial")
public class DormAndMealPlanCalculator extends JFrame{
 
	private JLabel label; // Displays a message
	private JPanel dormPanel; // To hold dorm panel components
	private JPanel selectedDormPanel; // To hold selected dorm components
	private JComboBox dormBox; // A list of Dorm
	private JTextField selectedDorm; // Selected dorm
 
	private JPanel mealPanel; // To hold meal panel components
	private JPanel selectedMealPanel; // To hold selected meal panel components
	private JComboBox mealBox; // A list of meal plan
	private JTextField selectedMeal; // Selected meal plan
 
	private JPanel totalPanel; // To hold total panel component
	private JTextField total; // Total Plan 
 
	// The following array holds the values that will
	// be displayed in the dormBox combo box.
 
	// Dorm Hall
	private String[] dorm = {
			"Allen Hall $" + 1500,
			"Pike Hall $" + 1600,
			"Farthing Hall $" + 1200,
			"University Suites $" + 1800
	};
	// Dorm Rate
	double[] drate = {1500, 1600, 1200, 1800};
 
	//Meal plans
	private String[] meal = { 
			"7 Meals Per Week $" + 560,
			"14 meals per week $" + 1095,
			"Unlimited meals $" + 1500
	};
	//Meal plan rate
	double [] mrate = {560, 1095, 1500};
	
	//*******************************************
	// Constructor								*
	//*******************************************
	
	public DormAndMealPlanCalculator(){
	// Set the title.
	setTitle("Dorm and Meal Plan Calculator");
 
	// Specify an action for the close button.
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
	// Create a BorderLayout manager.
	setLayout(new GridLayout(3,3));
 
	// Build the panels.
	buildDormPanel();
	buildSelectedDormPanel();
	buildMealPanel();
	buildSelectedMealPanel();
	buildTotalPanel();
 
	// Add the panels to the content pane.
	add(dormPanel);
	add(selectedDormPanel);
	add(mealPanel);
	add(selectedMealPanel);
	add(totalPanel);
 
	// Pack and display the window.
	pack();
	setVisible(true);
	}
 
	// The buildDormPanel method adds a combo box 
	// with the types of dorm to a panel.
 
	private void buildDormPanel(){
		label = new JLabel("Select Hall: ");
		// Create a panel to hold the combo box.
		dormPanel = new JPanel();
		// Create the combo box
		dormBox = new JComboBox(dorm);
		// Register an action listener.
		dormBox.addActionListener(new ComboBoxListener());
		// Add the combo box to the panel.
		dormPanel.add(label);
		dormPanel.add(dormBox);
	}
	
	// The buildMealPanel method adds a combo box 
	// with the types of meal to a panel.
 
	private void buildMealPanel(){
		// Create a panel to hold the combo box.
		label = new JLabel("Select Meal Plan: ");
		mealPanel = new JPanel();
		// Create the combo box
		mealBox = new JComboBox(meal);
		// Register an action listener.
		mealBox.addActionListener(new ComboBoxListener());
		// Add the combo box to the panel.
		mealPanel.add(label); 
		mealPanel.add(mealBox);
	}
	
	// The buildSelectedDormPanel method adds a
	// read-only text field to a panel.
 
	private void buildSelectedDormPanel(){
		// Create a panel to hold the components.
		selectedDormPanel = new JPanel();
 
		// Create the label.
		label = new JLabel("Your Dorm: ");
 
		// Create the uneditable text field.
		selectedDorm = new JTextField(20);
		selectedDorm.setEditable(false);
 
		// Add the label and text field to the panel.
		selectedDormPanel.add(label);
		selectedDormPanel.add(selectedDorm);
	}
	
	// The buildSelectedMealPanel method adds a
	// read-only text field to a panel.
 
	private void buildSelectedMealPanel(){
		// Create a panel to hold the components.
		selectedMealPanel = new JPanel();
 
		// Create the label.
		label = new JLabel("Meal Plan is: ");
 
		// Create the uneditable text field.
		selectedMeal = new JTextField(20);
		selectedMeal.setEditable(false);
 
		// Add the label and text field to the panel.
		selectedMealPanel.add(label);
		selectedMealPanel.add(selectedMeal);
	}
 
	private void buildTotalPanel(){
		// Create a panel to hold the combo box.
		totalPanel = new JPanel();
 
		// Create the label.
		//totalPanel.setHorizontalAlignment(JPanel.CENTER);
		label = new JLabel("Your Total for the semester is: ");
 
		// Create the uneditable text field.
		total = new JTextField(15);
		total.setEditable(false);
	
		totalPanel.add(label);
		totalPanel.add(total); 
	}
 
	// Private inner class that handles the event when
	// the user selects an item from the combo box.
 
	private class ComboBoxListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
	
			int dorm = 0;
			int meal = 0;
 
			// Get the selected dorm.
			String selection1 =
				(String) dormBox.getSelectedItem();
				dorm = dormBox.getSelectedIndex();
 
			// Get the selected meal plan.
			String selection2 = 
				(String) mealBox.getSelectedItem();
				meal = mealBox.getSelectedIndex();
			
			// Add selections
			double selection3 = 
				(drate[dorm] + mrate[meal]);
			
			// Display the selected items in the text field.
			selectedDorm.setText(selection1);
			selectedMeal.setText(selection2); 
			total.setText("$" + selection3);
		}
	}
	// The main method creates an instance of the ComboBoxWindow
	// class which causes it to display its window.
	public static void main(String[] args){
		//display instructions
		JOptionPane.showMessageDialog(null, "Click on the Dorm" +
		" and Meal Plans that you would like.  " + 
		" The total price is then calculated for you.");
 
		@SuppressWarnings("unused")
		DormAndMealPlanCalculator mpc = new DormAndMealPlanCalculator();
	}
}

Open in new window

Avatar of Mick Barry
you can layout your gui however you want really

http://java.sun.com/docs/books/tutorial/uiswing/layout/

>  if the user wants to select the "default" then they would have to change one of the selections and then change it back.  Is there a way to "fix" these things?

sound slike you don't initially configure defaults for all components.
setting the combo selection in your code *after* you add your listeners should fix that

Avatar of b_acs

ASKER

> setting the combo selection in your code *after* you add your listeners should fix that

I'm sorry to ask, but what do you mean?

why does the user need to change the selection and change it back?  whats not getting set?

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of b_acs

ASKER

Ok I see about setting the defaults, but I think in this case I actually may want an empty default so I also added a new element to the beginning of the arrays that I left blank.

I'm still reading trying to figure out how to move my totalpane to the right side of the box.....


	// The following array holds the values that will
	// be displayed in the dormBox combo box, including an 
	// empty line for default initialization.
 
	// Dorm Hall
	private String[] dorm = {
			"", 
			"Allen Hall $" + 1500,
			"Pike Hall $" + 1600,
			"Farthing Hall $" + 1200,
			"University Suites $" + 1800
	};
	// Dorm Rate
	double[] drate = {0, 1500, 1600, 1200, 1800};
	
	// The following array holds the values that will
	// be displayed in the mealBox combo box, including an 
	// empty line for default initialization.
 
	//Meal plans
	private String[] meal = {
			"",
			"7 Meals Per Week $" + 560,
			"14 meals per week $" + 1095,
			"Unlimited meals $" + 1500
	};
	//Meal plan rate
	double [] mrate = {0, 560, 1095, 1500};

Open in new window

as you using a GridLayout for its container you should be able to move it by changing the order you add them (to the parent container). GridLayout fills row by row from left to right
Avatar of b_acs

ASKER

But it is the last one added.  And I want it to be on the bottom since its the total, but just trying to figure out how to put it on the right.   Currently looking at trying to change the formatting on that line to right to left instead of left to right
Avatar of b_acs

ASKER

do i need to create a flow layout class to do this?
hard to day without know the exact layout you are trying to achieve

> But it is the last one added.  And I want it to be on the bottom since its the total

can still be on the bottom, you just want it in the 1st cell of the bottom row by the sound of it
try adding it 4th

Avatar of b_acs

ASKER

I'm sorry....my grid is 3 rows by two cols and my preferred setup is:

Dorm Panel               Selected Dorm Panel
Meal Panel                Selected Meal Panel
                                 Total Panel

So I guess really I want it in the second cell of the bottom row.   Currently it is in the first cell
This is obviously a pretty minor issue, (has no impact on my grade....lol) I am just trying to figure out how to get it the way I would like it.....
you could add say an empty JLabel
or use a different layout manager

Avatar of b_acs

ASKER

Oh I see....I just read that I will have to use a different layout if I want to leave an empty cell.

Now to figure out which one.....lol
you could use a 2x1 gridlayout (for the two columns)
and in each column use a (vertical) BoxLayout
Avatar of b_acs

ASKER

For simplicity (getting tired...), I did add an empty JPanel and got what I wanted (basically).  
I think I will play with this some more to learn how to use some of the different layout managers.

Thank you for your time, objects, I really appreciate it!

Avatar of b_acs

ASKER

I'll look into your last suggestion as well
Avatar of b_acs

ASKER

Thanks again for your help!
You actually create one column too many in your GridLayout, but to bring over the total panel to where you want it, simply add a blank component before it:

        // Add the panels to the content pane.
        add(dormPanel);
        add(selectedDormPanel);
        add(mealPanel);
        add(selectedMealPanel);
        add(new JLabel(""));
        add(totalPanel);

Open in new window

glad I could help, let me know if you need any other help :)

b_acs, i'm a little confused as to how the accepted answer answers your question...
Avatar of b_acs

ASKER

CEHJ, I just picked one of objects comments.  Honestly I was a little lazy, because I should have split it, but since all of my questions were answered by objects I just picked the first comment of his that answered one of my questions.
That's OK. Anyway, the adjustments i mentioned will fix your main problem
Avatar of b_acs

ASKER

Oh and as for the making one column too many, I didn't notice that I hadn't changed that back before posting my code.  I was playing with the number of columns and rows trying to see the difference.  

Thanks for noticing that...
Avatar of b_acs

ASKER

Thank you!  I did mention that I did what you suggested already.  I think I said JPanel instead of JLabel, though.  But that's because I actually added an empty panel instead of a blank label.  Doing a blank JLabel is a bit simpler though.