School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,132 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,924 people online right now. Registration is fast and FREE... Join Now!



Cafe Java Applet

Cafe Java Applet need help making pull down lists Rate Topic: -----

#1 67039  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 12
  • Joined: 27-February 09


Dream Kudos: 0

Post icon  Posted 11 May 2009 - 05:22 PM

hi, I need assistance on my homework. I am supposed to create a pizza java applet with 2 pull downs. I can not figure out how to make them pull downs. Any ideas? This program works as a checklist but I can not figure out the pull down method. Any help would be appreciated.

thanks again

 

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class pizza extends Applet implements ItemListener{
  private Checkbox pepperoni, olive, onion, sausage, peppers, cheese;
  private Checkbox ham, pineapple, anchovies;
  private TextField t;
  private CheckboxGroup pizzaSize;
  private Checkbox small, medium, large, ex_large;
  float price = 7.00f, valPep = 0, valOli = 0, valOni = 0, valSau = 0;
  float valPpp = 0, valChe = 0, valHam = 0, valPin = 0, valAnc = 0;
  float size = 7.00f;

  public void init() {
  
	setLayout(new GridLayout(13,1));
	pizzaSize = new CheckboxGroup();
	add(new Label("Welcome to my Pizzeria, what size pizza would you like?", Label.CENTER));
	small = new Checkbox("Small", pizzaSize, false);
	small.addItemListener(this);
	add(small);
	medium = new Checkbox("Medium", pizzaSize, false);
	medium.addItemListener(this);
	add(medium);
	large = new Checkbox("Large", pizzaSize, false);
	large.addItemListener(this);
	add(large);
	ex_large = new Checkbox("Extra Large", pizzaSize, false);
	ex_large.addItemListener(this);
	add(ex_large);
	add(new Label("Please select your toppings? ($1.00 additional per topping)",
Label.CENTER));
	pepperoni = new Checkbox("Pepperoni");
	pepperoni.addItemListener(this);
	add(pepperoni);
	olive = new Checkbox("Olives");
	olive.addItemListener(this);
	add(olive);
	onion = new Checkbox("Onions");
	onion.addItemListener(this);
	add(onion);
	sausage = new Checkbox("Sausage");
	sausage.addItemListener(this);
	add(sausage);
	peppers = new Checkbox("Peppers");
	peppers.addItemListener(this);
	add(peppers);
	cheese = new Checkbox("Extra Cheese");
	cheese.addItemListener(this);
	add(cheese);
	ham = new Checkbox("Ham");
	ham.addItemListener(this);
	add(ham);
	pineapple = new Checkbox("Pineapple");
	pineapple.addItemListener(this);
	add(pineapple);
	anchovies = new Checkbox("Anchovies");
	anchovies.addItemListener(this);
	add(anchovies);
	
	t = new TextField("$" + String.valueOf(price));
	t.setEditable(false); 
	add(t);
  }
  
  public void itemStateChanged (ItemEvent e) {

	  if(e.getSource() == small){
	  price -= size;
	  size = 7.00f;
	  price += size;
	}
	  if(e.getSource() == medium){
 	  price -= size;
		size = 9.00f;
	  price += size;
	}
	  if(e.getSource() == large){
	  price -= size;
	  size = 11.00f;
	  price += size;
	}
	if(e.getSource() == ex_large){
	  price -= size;
	  size = 14.00f;
	  price += size;
	}
	  if(e.getSource() == pepperoni){
		valPep = (pepperoni.getState() ?  1.00f : -1.00f);
		price += valPep;
	  }
	  if(e.getSource() == olive){
		valOli = (olive.getState() ?  1.00f : -1.00f);
		price += valOli;
	}
	  if(e.getSource() == onion){
	  valOni = (onion.getState() ?  1.00f : -1.00f);
	  price += valOni;
	}
	  if(e.getSource() == sausage){
	  valSau = (sausage.getState() ?  1.00f : -1.00f);
	  price += valSau;
	}
	if(e.getSource() == peppers){
	  valPpp = (peppers.getState() ?  1.00f : -1.00f);
	  price += valPpp;
	}
	if(e.getSource() == cheese){
		valChe = (cheese.getState() ?  1.00f : -1.00f);
	  price += valChe;
	}
	if(e.getSource() == ham){
		valHam = (ham.getState() ?  1.00f : -1.00f);
	  price += valHam;	
	}
	if(e.getSource() == pineapple){
		valPin = (pineapple.getState() ?  1.00f : -1.00f);
	  price += valPin;
	}
	if(e.getSource() == anchovies){
		valAnc = (anchovies.getState() ?  1.00f : -1.00f);
	  price += valAnc;
	}
	  t.setText("$" + String.valueOf(price));
	  return;
  }
  
 public boolean handleCheckbox(Checkbox c){
	  if(c.getState())
	  price += 1.00f;
	else
	  price -= 1.00f;
	t.setText("$" + String.valueOf(price));
	return true;
 }
}


Was This Post Helpful? 0
  • +
  • -


#2 pbl  Icon User is offline

  • Java Lover
  • Icon
  • Group: Mentors
  • Posts: 11,168
  • Joined: 06-March 08


Dream Kudos: 475

Posted 11 May 2009 - 06:24 PM

What you call a "pull down" is a JComboBox

http://java.sun.com/.../JComboBox.html

String[] size = {"Small", "Medium", "Large"};
double[] price = {10.0, 12.0, 15.0};
JComboBox combo = new JComboBox(size);

// in your action listener when you determine the price
int index = combo.getSelectedIndex();
double total = price[index];	// get base price according to size



Happy coding
Was This Post Helpful? 0
  • +
  • -

#3 67039  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 12
  • Joined: 27-February 09


Dream Kudos: 0

Posted 11 May 2009 - 06:53 PM

hmm, did I enter it in the wrong area, I am getting error codes
I inserted the code and tookout the checkbox fields.



import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class pizzab extends Applet implements ItemListener{
  private Checkbox pepperoni, olive, onion, sausage, peppers, cheese;
  private Checkbox ham, pineapple, anchovies;
  private TextField t;
  JComboBox combo = new JComboBox(size);

  String[] size = {"Small", "Medium", "Large", "Xtra Large"};
double[] price = {7.0, 9.0, 11.0, 14.0};


// in your action listener when you determine the price
int index = combo.getSelectedIndex();
double total = price[index];	// get base price according to size

  

  public void init() {
  
	setLayout(new GridLayout(13,1));
	pizzaSize = new CheckboxGroup();
	add(new Label("Welcome to my Pizzeria, what size pizza would you like?", Label.CENTER));
	small = new Checkbox("Small", pizzaSize, false);
	small.addItemListener(this);
	add(small);
	medium = new Checkbox("Medium", pizzaSize, false);
	medium.addItemListener(this);
	add(medium);
	large = new Checkbox("Large", pizzaSize, false);
	large.addItemListener(this);
	add(large);
	ex_large = new Checkbox("Extra Large", pizzaSize, false);
	ex_large.addItemListener(this);
	add(ex_large);
	add(new Label("Please select your toppings? ($1.00 additional per topping)",
Label.CENTER));
	pepperoni = new Checkbox("Pepperoni");
	pepperoni.addItemListener(this);
	add(pepperoni);
	olive = new Checkbox("Olives");
	olive.addItemListener(this);
	add(olive);
	onion = new Checkbox("Onions");
	onion.addItemListener(this);
	add(onion);
	sausage = new Checkbox("Sausage");
	sausage.addItemListener(this);
	add(sausage);
	peppers = new Checkbox("Peppers");
	peppers.addItemListener(this);
	add(peppers);
	cheese = new Checkbox("Extra Cheese");
	cheese.addItemListener(this);
	add(cheese);
	ham = new Checkbox("Ham");
	ham.addItemListener(this);
	add(ham);
	pineapple = new Checkbox("Pineapple");
	pineapple.addItemListener(this);
	add(pineapple);
	anchovies = new Checkbox("Anchovies");
	anchovies.addItemListener(this);
	add(anchovies);
	
	t = new TextField("$" + String.valueOf(price));
	t.setEditable(false); 
	add(t);
  }
  
  public void itemStateChanged (ItemEvent e) {

	  if(e.getSource() == small){
	  price -= size;
	  size = 7.00f;
	  price += size;
	}
	  if(e.getSource() == medium){
 	  price -= size;
		size = 9.00f;
	  price += size;
	}
	  if(e.getSource() == large){
	  price -= size;
	  size = 11.00f;
	  price += size;
	}
	if(e.getSource() == ex_large){
	  price -= size;
	  size = 14.00f;
	  price += size;
	}
	  if(e.getSource() == pepperoni){
		valPep = (pepperoni.getState() ?  1.00f : -1.00f);
		price += valPep;
	  }
	  if(e.getSource() == olive){
		valOli = (olive.getState() ?  1.00f : -1.00f);
		price += valOli;
	}
	  if(e.getSource() == onion){
	  valOni = (onion.getState() ?  1.00f : -1.00f);
	  price += valOni;
	}
	  if(e.getSource() == sausage){
	  valSau = (sausage.getState() ?  1.00f : -1.00f);
	  price += valSau;
	}
	if(e.getSource() == peppers){
	  valPpp = (peppers.getState() ?  1.00f : -1.00f);
	  price += valPpp;
	}
	if(e.getSource() == cheese){
		valChe = (cheese.getState() ?  1.00f : -1.00f);
	  price += valChe;
	}
	if(e.getSource() == ham){
		valHam = (ham.getState() ?  1.00f : -1.00f);
	  price += valHam;	
	}
	if(e.getSource() == pineapple){
		valPin = (pineapple.getState() ?  1.00f : -1.00f);
	  price += valPin;
	}
	if(e.getSource() == anchovies){
		valAnc = (anchovies.getState() ?  1.00f : -1.00f);
	  price += valAnc;
	}
	  t.setText("$" + String.valueOf(price));
	  return;
  }
  
 public boolean handleCheckbox(Checkbox c){
	  if(c.getState())
	  price += 1.00f;
	else
	  price -= 1.00f;
	t.setText("$" + String.valueOf(price));
	return true;
 }
}


View Postpbl, on 11 May, 2009 - 06:24 PM, said:

What you call a "pull down" is a JComboBox

http://java.sun.com/.../JComboBox.html

String[] size = {"Small", "Medium", "Large"};
double[] price = {10.0, 12.0, 15.0};
JComboBox combo = new JComboBox(size);

// in your action listener when you determine the price
int index = combo.getSelectedIndex();
double total = price[index];	// get base price according to size



Happy coding

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • Java Lover
  • Icon
  • Group: Mentors
  • Posts: 11,168
  • Joined: 06-March 08


Dream Kudos: 475

Posted 11 May 2009 - 07:07 PM

 JComboBox combo = new JComboBox(size);

  String[] size = {"Small", "Medium", "Large", "Xtra Large"};



size has to be defined BEFORE you can use it as constructor for the combo :)
  String[] size = {"Small", "Medium", "Large", "Xtra Large"};
 JComboBox combo = new JComboBox(size);



*Edited damn English it is "as" not "has"

This post has been edited by pbl: 11 May 2009 - 07:08 PM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month