Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-24-2011, 12:53 AM   PM User | #1
beast543
New to the CF scene

 
Join Date: Apr 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
beast543 is an unknown quantity at this point
isSelected not finding selected radio button

My program is determining the price per ticket based on the selected radio button, but no matter the button selected the senior rate is always used. Any idea where I went wrong?

Code:
//import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;

public class EncoreMoviesMenuValidationApp
{
	public static void main(String[] args)
	{
		JFrame frame = new EncoreFrame();
		frame.setVisible(true);
	}//end of main
}//end of class

class EncoreFrame extends JFrame
{
	public EncoreFrame()
	{
		setTitle("Encore Movies");
		setSize(350, 450);
		setLocationRelativeTo(null);
		setResizable(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new EncorePanel(this);
		add(panel);
	}//end of EncoreFrame()
}//end of EncoreFrame class

class EncorePanel extends JPanel implements ActionListener
{
	private JLabel movieNameLabel;
	private JRadioButton child, adult, senior, empty;
	private JMenuBar menuBar;
	private JMenu menuFile, menuHelp;
	private JMenuItem menuFileCommands,menuFileExit,menuFileCommandsReset, menuFileCommandsCalculate, menuHelpAbout;
	private JButton calculateButton, resetButton, ExitButton;
	private JComboBox movieNameComboBox, ticketsComboBox;
	private JCheckBox Matinee;
	private JTextArea output;
	String[] movies = {"","The Fighter","127 Hours","Restrepo","The King's Speech"};

	public EncorePanel(JFrame frame)//passing in frame so that you can set the JMenuBar
	{
		//construct menubar
		menuBar = new JMenuBar();

		//construct the menus
		menuFile = new JMenu("File");
		menuFile.setMnemonic(KeyEvent.VK_F);
		menuHelp = new JMenu("Help");
		menuHelp.setMnemonic(KeyEvent.VK_H);

		//add menu to the menuBar
		menuBar.add(menuFile);
		menuBar.add(menuHelp);

		//create menus for menus
		menuFileCommands = new JMenu("Commands");
		menuFileExit = new JMenuItem("Exit");
		menuFileExit.setMnemonic(KeyEvent.VK_X);
		menuFileExit.setAccelerator(KeyStroke.getKeyStroke("ctrl Q"));
		menuFileExit.addActionListener(this);
		menuHelpAbout = new JMenuItem("About");
		menuHelpAbout.setMnemonic(KeyEvent.VK_A);
		menuHelpAbout.setAccelerator(KeyStroke.getKeyStroke("F1"));
		menuHelpAbout.addActionListener(this);

		//create items for menu
		menuFileCommandsCalculate = new JMenuItem("Calculate");
		menuFileCommandsCalculate.setAccelerator(KeyStroke.getKeyStroke("ctrl L"));
		menuFileCommandsCalculate.addActionListener(this);
		menuFileCommandsReset = new JMenuItem("Reset");
		menuFileCommandsReset.setAccelerator(KeyStroke.getKeyStroke("ctrl R"));
		menuFileCommandsReset.addActionListener(this);

		menuFile.add(menuFileCommands);
		menuFileCommands.add(menuFileCommandsCalculate);
		menuFileCommands.add(menuFileCommandsReset);
		menuFile.add(menuFileExit);

		menuHelp.add(menuHelpAbout);

		//add menuBar to the frame
		frame.setJMenuBar(menuBar);

		//DECLARING THE PANELS AND SETTING LAYOUTS
		JPanel containerPane = new JPanel();
		JPanel movieNamePanel = new JPanel();
		JPanel ticketPanel = new JPanel();
		JPanel displayPanelRadio = new JPanel();
		JPanel outputPanel = new JPanel();
		JPanel buttonPanel = new JPanel();

		movieNamePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		ticketPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		displayPanelRadio.setLayout(new FlowLayout(FlowLayout.CENTER));
		outputPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

		containerPane.setLayout(new BoxLayout(containerPane, BoxLayout.PAGE_AXIS));

		JLabel movieNameLabel = new JLabel("Movie Name: ");
		movieNameComboBox = new JComboBox(movies);

		movieNamePanel.add(movieNameLabel);
		movieNamePanel.add(movieNameComboBox);

		//declare radiobuttons 
		child = new JRadioButton("Child", false);
		adult = new JRadioButton("Adult", false);
		senior = new JRadioButton("Senior", false);
		empty = new JRadioButton("",true);
		empty.setVisible(false);

		child.addActionListener(this);
		adult.addActionListener(this);
		senior.addActionListener(this);



		ButtonGroup radioGroup = new ButtonGroup();

		radioGroup.add(child);
		radioGroup.add(adult);
		radioGroup.add(senior);
		radioGroup.add(empty);

		//displayPanelRadio.add(radioGroup);

		displayPanelRadio.add(child);
		displayPanelRadio.add(adult);
		displayPanelRadio.add(senior);

		//making a border around my radio button display panel
		Border etchedBdr = BorderFactory.createEtchedBorder();
		Border titledBdr = BorderFactory.createTitledBorder(etchedBdr, "Ticket Type");
		Border emptyBdr = BorderFactory.createEmptyBorder(10, 10, 10, 10);
		Border compoundBdr=BorderFactory.createCompoundBorder(titledBdr, emptyBdr);

		displayPanelRadio.setBorder(compoundBdr);

		String[] tickets = {"","1","2","3","4","5","6","7","8","9","10"};

		JLabel numberOfTicketsLabel = new JLabel("Number of Tickets: ");
		ticketsComboBox = new JComboBox(tickets);
		Matinee = new JCheckBox("Matinee");
		ticketPanel.add(numberOfTicketsLabel);
		ticketPanel.add(ticketsComboBox);
		ticketPanel.add(Matinee);

		//textarea output declaration and add to outputPanel
		output = new JTextArea(11,25);
		outputPanel.add(output);
		output.setBorder(BorderFactory.createEtchedBorder());
		output.setEditable(false);
		output.setFocusable(false);

		calculateButton = new JButton("Calculate Price");
		resetButton = new JButton("Reset");
		ExitButton = new JButton("Exit");

		//addActionListener to 3 buttons in buttonPanel
		calculateButton.addActionListener(this);
		resetButton.addActionListener(this);
		ExitButton.addActionListener(this);



		buttonPanel.add(calculateButton);
		buttonPanel.add(resetButton);
		buttonPanel.add(ExitButton);
		containerPane.add(movieNamePanel);
		containerPane.add(displayPanelRadio);
		containerPane.add(ticketPanel);

		containerPane.add(outputPanel);
		containerPane.add(buttonPanel);

		add(containerPane);

}//end of encorePanel constructor

public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		boolean validData = true;


		//if the calculate button or menuItem is clicked validation
		if(source == calculateButton || source == menuFileCommandsCalculate )
		{
			double cost = 0.0;
			String type = "";

			try
			{
				if(movieNameComboBox.getSelectedIndex() == 0)
				{
					JOptionPane.showMessageDialog(null, "Please select a Movie.", "Error", JOptionPane.ERROR_MESSAGE);
					movieNameComboBox.requestFocus();
					validData = false;
				}//end movie if
				else if(empty.isSelected())
				{
					JOptionPane.showMessageDialog(null, "Please select a Ticket Type.", "Error", JOptionPane.ERROR_MESSAGE);
					empty.requestFocus();
					validData = false;
				}//end movie if

				else if(ticketsComboBox.getSelectedIndex() == 0)
				{
					JOptionPane.showMessageDialog(null, "Please select the Number of Tickets you wish to purchase.", "Error", JOptionPane.ERROR_MESSAGE);
					ticketsComboBox.requestFocus();
					validData = false;
				}


				if(Matinee.isSelected())
				{
					if(child.isSelected())
					{
						cost = Double.parseDouble((String)ticketsComboBox.getSelectedItem()) * 2.5;
						type = child.getText();
					}
					else if(adult.isSelected())
					{
						cost = Double.parseDouble((String)ticketsComboBox.getSelectedItem()) * 5;
						type = adult.getText();
					}
					else if(senior.isSelected());
					{
						cost = Double.parseDouble((String)ticketsComboBox.getSelectedItem()) * 3;
						type = senior.getText();
					}
				}//end of if statements
			else
			{
				if(child.isSelected())
				{
					cost = Double.parseDouble((String)ticketsComboBox.getSelectedItem()) * 5;
					type = child.getText();
				}
				else if(adult.isSelected())
				{
					cost = Double.parseDouble((String)ticketsComboBox.getSelectedItem()) * 7.5;
					type = adult.getText();
				}
				else if(senior.isSelected());
				{
					cost = Double.parseDouble((String)ticketsComboBox.getSelectedItem()) * 6;
					type = senior.getText();
				}
			}//end else

				NumberFormat currency = NumberFormat.getCurrencyInstance();
				calculateButton.setText("Additional Tickets?");

				output.append((String)ticketsComboBox.getSelectedItem() + " " + type + " tickets for " + (String)movieNameComboBox.getSelectedItem() + " will cost " + currency.format(cost) + ".\n" );

		}//end try
		catch(NumberFormatException err)
		{
		}//end catch


		}//end if

		else if(source == resetButton || source == menuFileCommandsReset)
		{
			movieNameComboBox.setSelectedIndex(0);
			empty.setSelected(true);
			ticketsComboBox.setSelectedIndex(0);
			output.setText(null);
			calculateButton.setText("Calculate Price");
			Matinee.setSelected(false);
		}
		else if(source == ExitButton || source == menuFileExit)
		{
			if (JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION)
			{
				System.exit(0);
			}//end if JOptionPane
		}//end else exitButton
		else if(source == menuHelpAbout)
		{
			JOptionPane.showMessageDialog(null, "Programmed by *******\nDate\n***", "About", JOptionPane.INFORMATION_MESSAGE);
		}//end else if

	}//and of actionPerformed();

}// end of EncorePanel class

Last edited by beast543; 04-24-2011 at 01:41 PM..
beast543 is offline   Reply With Quote
Old 04-26-2011, 04:50 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,649
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Twice you have this:
Code:
else if(senior.isSelected());// <--
Regardless of a branching, a semi-colon at the end of a control structure is always true. So no matter if its a while loop, a for loop, an else if, or anything, it will always evaluate it once. The exception is the do/while loop since the do will not accept a semi colon (compile error), and the while requires one.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:43 PM.


Advertisement
Log in to turn off these ads.