By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,077 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,077 IT Pros & Developers. It's quick & easy.

What does "java.awt.Button cannot be cast to javax.swing.JComboBox" mean?

P: 13
i have this unfinished java program and i can't figure out what is the problem..

please help...

/**
* @(#)Answer3.java
*
*
* @author
* @version 1.00 2008/1/17
*/
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.border.*;

public class Answer3 extends Applet implements ActionListener {

//declaration of instance variables
CheckboxGroup radioGroup2;
TextField tf1, tf2, tf3;
JComboBox flavorsCombo;
Button addPizza;
Checkbox Personal, Regular, Family;
Checkbox Bacon, Beef, Ham, Pepperoni, Onions,Pineapple, Cheese, Mushroom;
int pricePersonal = 150,
priceRegular = 250,
priceFamily = 400,
priceTopping = 20,
total = 0;

public void init() {

JPanel panel = new JPanel();
//displays the name of the program
JLabel programName = new JLabel (" Pizza Station Ordering Software ");
programName.setFont (new Font ("Arial", Font.BOLD, 18));
panel.add(programName);
panel.setBorder(new EtchedBorder());
panel.setLayout(new GridLayout(1,5,20,50));
add(panel);

JPanel panel1 = new JPanel();
//initializes the JCombobox
flavorsCombo = new JComboBox();
flavorsCombo.addItem("Hawaiian");
flavorsCombo.addItem("Mozzarella");
flavorsCombo.addItem("Pepperoni");
panel1.add(flavorsCombo);
flavorsCombo.addActionListener(this);
panel1.setBorder(new TitledBorder(new EtchedBorder(), "Pizza Flavors"));
add(panel1);

//declares a new category of CheckboxGroup
CheckboxGroup radioGroup1 = new CheckboxGroup();

//initializes the radio buttons for radioGroup1
Personal = new Checkbox("Personal",radioGroup1,true);
Regular = new Checkbox("Regular",radioGroup1,false);
Family = new Checkbox("Family",radioGroup1,false);

//initializes the radio buttons for radioGroup2
Bacon = new Checkbox("Bacon",radioGroup2,false);
Beef = new Checkbox("Beef ",radioGroup2,false);
Ham = new Checkbox("Ham ",radioGroup2,false);
Pepperoni = new Checkbox("Pepperoni ",radioGroup2,false);
Onions = new Checkbox("Onions ",radioGroup2,false);
Pineapple = new Checkbox("Pineapple ",radioGroup2,false);
Cheese = new Checkbox("Cheese ",radioGroup2,false);
Mushroom = new Checkbox("Mushroom ",radioGroup2,false);

//TitledBorder which displays the radioButtons of CheckboxGroup1
JPanel panel2 = new JPanel();
panel2.add(Personal);
panel2.add(Regular);
panel2.add(Family);
panel2.setBorder(new TitledBorder(new EtchedBorder(), "Pizza Flavors"));
add(panel2);

//TitledBorder which displays the radioButtons of CheckboxGroup2
JPanel panel3 = new JPanel();
panel3.add(Bacon);
panel3.add(Beef);
panel3.add(Ham);
panel3.add(Pepperoni);
panel3.add(Onions);
panel3.add(Pineapple);
panel3.add(Cheese);
panel3.add(Mushroom);
panel3.setBorder(new TitledBorder(new EtchedBorder(), "ExtraToppings"));
panel3.setLayout(new GridLayout(3,3,10,2));
add(panel3);

JPanel panel4 = new JPanel();

JPanel panel5 = new JPanel();
JLabel quantity = new JLabel ("Quantity ");
quantity.setFont (new Font ("Arial", Font.BOLD, 12));
panel4.add(quantity);
//initializes the TextField
tf1 = new TextField(5);
tf1.setEditable(true);
panel5.add(tf1);
panel5.setBorder(new EmptyBorder(2,10,2,2));
panel4.add(panel5);

JPanel panel6 = new JPanel();

//initializes the addPizza button
addPizza = new Button("Add Pizza to Order List");
addPizza.addActionListener(this);
panel6.add(addPizza);
panel6.setBorder(new EmptyBorder(2,2,2,24));
panel4.add(panel6);
panel4.setBorder(new EtchedBorder());
add(panel4);

//TitledBorder which displays the JTextArea
JPanel panel7 = new JPanel();

//initializes the JTextArea
JTextArea pizzaOrders = new JTextArea(8,28);
pizzaOrders.setEditable(false);
panel7.add(pizzaOrders);
panel7.setBorder(new TitledBorder(new EtchedBorder(), "Pizza Orders"));
add(panel7);

JPanel panel8 = new JPanel();

//initializes the name entry
JLabel name = new JLabel("Name ");
name.setFont(new Font("Arial",Font.BOLD,12));
panel8.add(name);
tf2 = new TextField(19);
panel8.add(tf2);
panel8.setBorder(new EtchedBorder());
add(panel8);

JPanel panel9 = new JPanel();


JLabel total = new JLabel("Total ");
total.setFont(new Font("Arial",Font.BOLD,12));
panel9.add(total);
tf3 = new TextField(5);
tf3.setEditable(false);
panel9.add(tf3);
panel9.setBorder(new EtchedBorder());
add(panel9);

}


public void actionPerformed(ActionEvent e){
JComboBox cb = (JComboBox)e.getSource();
String flavorPizza = (String)cb.getSelectedItem();



if (e.getSource() == addPizza)
JOptionPane.showMessageDialog(null,"NONSENSE");



}

}
Jan 19 '08 #1
Share this Question
Share on Google+
2 Replies


Expert 10K+
P: 11,448
I'm sure your compiler mentioned a line number when it bumped in to that problem.
If/when you want your problem solved on this forum please supply us with all the
information you received when you hit the problem. A quick hint: you are messing
with AWT objects as well as Swing objects and that is generally not done. A
Button in the AWT library most certainly is not a Swing JComboBox component.
You try to treat one as the other somewhere in your code.

kind regards,

Jos
Jan 19 '08 #2

P: 13
I'm sure your compiler mentioned a line number when it bumped in to that problem.
If/when you want your problem solved on this forum please supply us with all the
information you received when you hit the problem. A quick hint: you are messing
with AWT objects as well as Swing objects and that is generally not done. A
Button in the AWT library most certainly is not a Swing JComboBox component.
You try to treat one as the other somewhere in your code.

kind regards,

Jos
Thanks Jos!I did some confusing move but i did finish this program...! Congratulations self!
Jan 27 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.