gathering the data
how do i make it so that when the radio button is clicked, a string of text is displayed below it? everytime i make an action listener for one of the radio buttons, my application won't run
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclass swingMakeupimplements ItemListener{
JPanel cards;//a panel that uses CardLayout
finalstatic String BUTTONPANEL ="Meats";
finalstatic String TEXTPANEL ="Name Of Customer";
finalstatic String TEXTPANEL2 ="Number of Subs";
finalstatic String CHECKPANEL ="Fixins'";
JRadioButton beef, turkey, sheep;
JTextField name, number;
publicvoid addComponentToPane(Container pane){
//Put the JComboBox in a JPanel
JPanel comboBoxPane =new JPanel();
String comboBoxItems[] ={ BUTTONPANEL, TEXTPANEL, TEXTPANEL2, CHECKPANEL};
JComboBox cb =new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
//Create the cards
JPanel card1 =new JPanel();
card1.add(beef=new JRadioButton("Beef Lips -- $5.00"));
card1.add(turkey=new JRadioButton("Turkey Liver -- $4.00"));
card1.add(sheep=new JRadioButton("Sheep Stomach -- $6.00"));
JPanel card2 =new JPanel();
card2.add(name=new JTextField("", 20));
JPanel card3 =new JPanel();
card3.add(number=new JTextField("", 20));
JPanel card4 =new JPanel();
card4.add(new JCheckBox("Lettuce"));
card4.add(new JCheckBox("Tomato"));
card4.add(new JCheckBox("Onion"));
card4.add(new JButton("Submit Order"));
//Create the panel that contains the cards
cards =new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);
cards.add(card3, TEXTPANEL2);
cards.add(card4, CHECKPANEL);
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
}
publicvoid itemStateChanged(ItemEvent evt){
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}
privatestaticvoid createAndShowGUI(){
JFrame frame =new JFrame("swingMakeup");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
swingMakeup demo =new swingMakeup();
demo.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
publicstaticvoid main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
createAndShowGUI();
}
});
}
}
# 4
i did, this is what i get in the console window
at swingMakeup.addComponentToPane(swingMakeup.java:28)
at swingMakeup.createAndShowGUI(swingMakeup.java:85)
at swingMakeup.access$000(swingMakeup.java:11)
at swingMakeup$1.run(swingMakeup.java:96)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
# 6
/*
* Matthew Tye
* Makeup Lab
* 12-7-06
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class swingMakeup implements ItemListener, ActionListener{
JPanel cards; //a panel that uses CardLayout
final static String BUTTONPANEL = "Meats";
final static String TEXTPANEL = "Name Of Customer";
final static String TEXTPANEL2 = "Number of Subs";
final static String CHECKPANEL = "Fixins'";
JRadioButton beef, turkey, sheep;
JTextField name, number;
public void addComponentToPane(Container pane) {
//Put the JComboBox in a JPanel
JPanel comboBoxPane = new JPanel();
String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL, TEXTPANEL2, CHECKPANEL };
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
beef.addActionListener(this);
comboBoxPane.add(cb);
//Create the cards
JPanel card1 = new JPanel();
card1.add(beef=new JRadioButton
("Beef Lips -- $5.00"));
card1.add(turkey=new JRadioButton
("Turkey Liver -- $4.00"));
card1.add(sheep=new JRadioButton
("Sheep Stomach -- $6.00"));
JPanel card2 = new JPanel();
card2.add(name=new JTextField("", 20));
JPanel card3 = new JPanel();
card3.add(number=new JTextField("", 20));
JPanel card4 = new JPanel();
card4.add(new JCheckBox("Lettuce"));
card4.add(new JCheckBox("Tomato"));
card4.add(new JCheckBox("Onion"));
card4.add(new JButton("Submit Order"));
//Create the panel that contains the cards
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);
cards.add(card3, TEXTPANEL2);
cards.add(card4, CHECKPANEL);
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
}
public void actionPerformed (ActionEvent e)
{
if(e.getSource()==beef){
}
}
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("swingMakeup");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
swingMakeup demo = new swingMakeup();
demo.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}