Basic Action Listener not workin
[nobr]This may seem like a lot of code, "BUT" there's one simple line that's causing the error and its way at the bottom inside the actionlistener event.
For some reason the button called "cmdSubmit" isn't registering in the ActionListener event. Any ideas why?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
publicclass Surveyextends JFrameimplements ActionListener
{
public Survey()
{
super("Health Awareness Survey");
//Place first Question
JLabel lblSleep =new JLabel("3: On average how many hours of sleep do you get per night?");
JComboBox cmbSleep =new JComboBox();
cmbSleep.addItem("3-5 hours");
cmbSleep.addItem("5-6 hours");
cmbSleep.addItem("6-7 hours");
cmbSleep.addItem("7-8 hours");
cmbSleep.addItem("> 9 hours");
cmbSleep.setEditable(false);// so user can't mess with the data
cmbSleep.setMaximumRowCount(3);
//adds the basic label + the comboBox
FlowLayout flowSleep =new FlowLayout(FlowLayout.LEFT,0,0);
JPanel pnlSleep =new JPanel(flowSleep);
pnlSleep.add(lblSleep);
pnlSleep.add(cmbSleep);
// Second Question
JLabel lblFood =new JLabel ("4. How many items of junk food do you consume per week?");
JRadioButton opt0Items =new JRadioButton("0-5 Items");
JRadioButton opt5Items =new JRadioButton("5-10 Items");
JRadioButton opt10Items =new JRadioButton("10-15 Items");
// create a button group for the radio buttons (so that you can only select 1 at a time)
ButtonGroup grpItems =new ButtonGroup();
grpItems.add(opt0Items);
grpItems.add(opt5Items);
grpItems.add(opt10Items);
FlowLayout flowItems =new FlowLayout(FlowLayout.LEFT, 0,0);// adds to the visuals ;)
JPanel pnlItems =new JPanel(flowItems);
pnlItems.add(lblFood);// adding labels and optionBoxes to the Panel
pnlItems.add(opt0Items);
pnlItems.add(opt5Items);
pnlItems.add(opt10Items);
//Third Question
JCheckBox chkHealthy =new JCheckBox("2. Is the majority of food you eat healthy?",false);
JLabel lblStatus =new JLabel("5: At what time(s) of the days do you brush your teeth?");
JCheckBox chkMorning =new JCheckBox ("1. Morning ");// creats a CheckBox
JCheckBox chkLunch =new JCheckBox ("2. Lunch ");// that isn't a family with the
JCheckBox chkNight =new JCheckBox ("3. Night");// other ones
// panel that will hold all the CheckBox components + the label
FlowLayout flowTeeth =new FlowLayout(FlowLayout.LEFT, 0,0);
JPanel pnlTeeth =new JPanel(flowItems);
pnlTeeth.add(lblStatus);
pnlTeeth.add(chkMorning);
pnlTeeth.add(chkLunch);
pnlTeeth.add(chkNight);
//Fourth Question
JLabel lblFav =new JLabel("1. What is your favorite food?");
JTextField txtFav =new JTextField(25);
txtFav.setToolTipText("Enter your favorite food");
//adds fourth question to the JPanel for later use
FlowLayout flowFood =new FlowLayout(FlowLayout.LEFT, 0,0);
JPanel pnlFav =new JPanel(flowFood);
pnlFav.add(lblFav);
pnlFav.add(txtFav);
JLabel lblDrinks =new JLabel ("6. What do you usually drink in the morning?");
String[] strDrink ={"Orange","Juice","Milk","Coffee","Tea","Other","can't remember"};
JList drink =new JList(strDrink);
// set how much space the list takes up
drink.setVisibleRowCount(2);
JScrollPane scrDrinks =new JScrollPane(drink, ScrollPaneConstants.
VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
// Label + box goes in the panel
FlowLayout flowDrinks =new FlowLayout(FlowLayout.LEFT, 0,0);
JPanel pnlDrinks =new JPanel(flowDrinks);
pnlDrinks.add(lblDrinks);
pnlDrinks.add(scrDrinks);
// ALL THE COMPONENTS ABOVE, get added to the main layout here
JPanel pnlFields =new JPanel(new GridLayout(5, 1));
pnlFields.add(pnlFav);
pnlFields.add(pnlSleep);
pnlFields.add(pnlItems);
pnlFields.add(pnlTeeth);
pnlFields.add(pnlDrinks);
pnlFav.add(chkHealthy);
//Comment Box + Button
JLabel lblFeedBack =new JLabel("give us");
// uses HTML code to produce a line ;)
lblFeedBack.setText("<html><hr>feedback you <br> would like to give us</html>");
lblFeedBack.setSize(10, 4);
JTextArea txtFeedBack =new JTextArea(4, 30);// creating a textbox
txtFeedBack.setLineWrap(true);// so that the text goes to next line when full
txtFeedBack.setWrapStyleWord(true);
txtFeedBack.setToolTipText("Enter your (Questions/Problems) here");
JScrollPane scrFeedBack =new JScrollPane(txtFeedBack, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JPanel pnlAddress =new JPanel(new BorderLayout());
pnlAddress.add(lblFeedBack, BorderLayout.CENTER);
JButton cmdSubmit =new JButton("Submit");
JButton cmdReset =new JButton("Reset");
JButton cmdExit =new JButton("Exit");
cmdSubmit.setMnemonic(KeyEvent.VK_G);// G instead of S, incase people think 'S = save'
cmdSubmit.setToolTipText("Click here to submit form");
cmdReset.setMnemonic(KeyEvent.VK_R);// hotkey for 'r'
cmdReset.setToolTipText("Click here to reset form");
cmdExit.setMnemonic(KeyEvent.VK_E);// adds hotkeys 'e'
cmdExit.setToolTipText("Click here to exit the program.");
JPanel pnlButtons =new JPanel(new GridLayout(1,0));
pnlButtons.add(cmdSubmit);
pnlButtons.add(cmdReset);
pnlButtons.add(cmdExit);
JPanel pnlFeedBack =new JPanel(new BorderLayout());
pnlFeedBack.add(pnlButtons, BorderLayout.SOUTH);
pnlFeedBack.add(scrFeedBack, BorderLayout.CENTER);
pnlFeedBack.add(lblFeedBack, BorderLayout.NORTH);
Container pane = this.getContentPane();
pane.setLayout(new BorderLayout());
pane.add(pnlFeedBack, BorderLayout.SOUTH);
pane.add(pnlFields, BorderLayout.CENTER);
//Action listeners
cmdSubmit.addActionListener(this);
cmdExit.addActionListener(this);
cmdReset.addActionListener(this);
//final configurations
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize (350, 500);
}
/*
* Performs tasks based on buttons pressed
*/
/**
* The Action Listener, takes in an ActionEvent.
*/
publicvoid actionPerformed(ActionEvent evt)
{
Object source = evt.getSource();
if (source == cmdSubmit)// error, same as below
{
}
elseif (evt.getSource() == cmdReset)// ERROR!!!! doesn't recognize "cmdReset" ?
{
}
}
}
Message was edited by:
Mornin_Javaing
Message was edited by:
Mornin_Javaing[/nobr]

