Solved

How do I change the layout of my combo boxes?

Posted on 2009-05-10
25
658 Views
Last Modified: 2013-11-23
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?
0
Comment
Question by:b_acs
  • 13
  • 9
  • 3
25 Comments
 

Author Comment

by:b_acs
ID: 24351466
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

0
 
LVL 92

Expert Comment

by:objects
ID: 24351479
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

0
 

Author Comment

by:b_acs
ID: 24351541
> 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?

0
 
LVL 92

Expert Comment

by:objects
ID: 24351551
why does the user need to change the selection and change it back?  whats not getting set?

0
 
LVL 92

Accepted Solution

by:
objects earned 350 total points
ID: 24351574

        // Set defaults

        mealBox.setSelectedIndex(0);
        dormBox.setSelectedIndex(0);

        // Pack and display the window.
        pack();
        setVisible(true);

0
 

Author Comment

by:b_acs
ID: 24351616
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

0
 
LVL 92

Expert Comment

by:objects
ID: 24351637
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
0
 

Author Comment

by:b_acs
ID: 24351671
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
0
 

Author Comment

by:b_acs
ID: 24351686
do i need to create a flow layout class to do this?
0
 
LVL 92

Expert Comment

by:objects
ID: 24351706
hard to day without know the exact layout you are trying to achieve

0
 
LVL 92

Expert Comment

by:objects
ID: 24351715
> 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

0
 

Author Comment

by:b_acs
ID: 24351754
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.....
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

by:objects
ID: 24351775
you could add say an empty JLabel
or use a different layout manager

0
 

Author Comment

by:b_acs
ID: 24351779
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
0
 
LVL 92

Expert Comment

by:objects
ID: 24351799
you could use a 2x1 gridlayout (for the two columns)
and in each column use a (vertical) BoxLayout
0
 

Author Comment

by:b_acs
ID: 24351801
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!

0
 

Author Comment

by:b_acs
ID: 24351805
I'll look into your last suggestion as well
0
 

Author Closing Comment

by:b_acs
ID: 31580024
Thanks again for your help!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24351876
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

0
 
LVL 92

Expert Comment

by:objects
ID: 24351885
glad I could help, let me know if you need any other help :)

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24351890
b_acs, i'm a little confused as to how the accepted answer answers your question...
0
 

Author Comment

by:b_acs
ID: 24352190
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24352208
That's OK. Anyway, the adjustments i mentioned will fix your main problem
0
 

Author Comment

by:b_acs
ID: 24352216
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...
0
 

Author Comment

by:b_acs
ID: 24352245
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.  
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now