10 Replies - 1494 Views - Last Post: 01 March 2009 - 02:35 PM Rate Topic: -----

#1 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

how do i add the action listener

Post icon  Posted 01 March 2009 - 08:26 AM

My teacher said to create a panel in different class. My confusion comes with the button handler. Does iit go in the main ptrogram or in the panel class.
I can't seem to figure it out
import java.awt.*;//Abstract windowing toolkit
import javax.swing.*; // 
import java.awt.event.*;

public class vendingmachine {
	
	//THE VARIABLES
	private Money moneyDisplay=new Money();
	private displayMenu candy= new displayMenu(100,50);
	private displayMenu chips = new displayMenu(100,65);
	private displayMenu Pop =new displayMenu(75,45);
	private displayMenu gum =new displayMenu(15,25);

	private ButtonHandler bHandler;
	
	public vendingmachine()
	{
		bHandler=new ButtonHandler();
	
		
		
		//listener
		exit.addActionListener(bHandler);

		candy.addActionListener(bHandler);
	
		
		chips.addActionListener(bHandler);
	
		
		Pop.addActionListener(bHandler);
		
		
		gum.addActionListener(bHandler);

	}//ends constructor vendingmachine
	//to handle the buttons
	private class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getActionCommand().equals("Exit"))
				System.exit(0);
			else if (e.getActionCommand().equals("Candy"))
				selection(candy,"Candy");
			else if (e.getActionCommand().equals("Chips"))
				selection(chips,"Chips");
			else if (e.getActionCommand().equals("Pop"))
				selection (Pop,"Pop");
			else if (e.getActionCommand().equals("Gum"))
					selection (gum,"Gum");
			
			
		}	
			
		}//end buttonhandler
	
	
	
	
	
	//method to  get item

	private void selection(displayMenu item, String string)
	{
		int coinsInserted=0;
		int price,coinsreq;
		String str;
		
		if (item.getCount()>0)
		{
			price=item.getProductCost();
			coinsreq = price - coinsInserted;
			
			while (coinsreq > 0)
			 {
				str = JOptionPane.showInputDialog("To buy "
									  + item
									  + " please insert "
									  + coinsreq + " cents");
				coinsInserted = coinsInserted
								+ Integer.parseInt(str);
				coinsreq = price - coinsInserted;
			}
			 moneyDisplay.acceptAmount(coinsInserted);
				item.makeSale();
				
				JOptionPane.showMessageDialog(null,"Please pick up your "
						+ item + " and enjoy",
						 "Thank you, Come again!",
						JOptionPane.PLAIN_MESSAGE);
		}
		
		 else		   //dispenser is empty
				JOptionPane.showMessageDialog(null,"Sorry "
									   + item
									   + " is sold out\n" +
									   "Make another selection",
									   "Thank you, Come again!",
									   JOptionPane.PLAIN_MESSAGE);
		
		
	}

	public static void main(String[] args)
	{
		JFrame frame =new JFrame();
		

		  frame.getContentPane().add(new Panelview());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
		
	}//end main

}//end class



Is This A Good Question/Topic? 0
  • +

Replies To: how do i add the action listener

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: how do i add the action listener

Posted 01 March 2009 - 10:17 AM

Well it depends on where all the buttons are. If you have all your buttons going on the panel, I would put the handler with the panel class. If you have all the buttons going into the main frame, keep it with the main program. If you have some buttons on the main frame and some on the panel, then you will need to make your handler public as a separate class (instead of private like you have) and then you can have it independent of the frame and the panel.

Ideally you want to keep the handler that handles the button in the same scope as where the button is located. If your buttons are going to be on the panel and that is where you have defined them (as members of the panel) then your handler should be in the panel class too.

Hopefully that answered your question. :)
Was This Post Helpful? 0
  • +
  • -

#3 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Re: how do i add the action listener

Posted 01 March 2009 - 11:18 AM

View PostMartyr2, on 1 Mar, 2009 - 09:17 AM, said:

Well it depends on where all the buttons are. If you have all your buttons going on the panel, I would put the handler with the panel class. If you have all the buttons going into the main frame, keep it with the main program. If you have some buttons on the main frame and some on the panel, then you will need to make your handler public as a separate class (instead of private like you have) and then you can have it independent of the frame and the panel.

Ideally you want to keep the handler that handles the button in the same scope as where the button is located. If your buttons are going to be on the panel and that is where you have defined them (as members of the panel) then your handler should be in the panel class too.

Hopefully that answered your question. :)

ok i got the exit button to work after put the handler in the panel.
but how do i pass the parameters to my selection method in the main.
this is my selection method in the main;
 
private void selection(displayMenu item, String string)
	{
		int coinsInserted=0;
		int price,coinsreq;
		String str;
		
		if (item.getCount()>0)
		{
			price=item.getProductCost();
			coinsreq = price - coinsInserted;
			
			while (coinsreq > 0)
			 {
				str = JOptionPane.showInputDialog("To buy "
									  + item
									  + " please insert "
									  + coinsreq + " cents");
				coinsInserted = coinsInserted
								+ Integer.parseInt(str);
				coinsreq = price - coinsInserted;
			}
			 moneyDisplay.acceptAmount(coinsInserted);
				item.makeSale();
				
				JOptionPane.showMessageDialog(null,"Please pick up your "
						+ item + " and enjoy",
						 "Thank you, Come again!",
						JOptionPane.PLAIN_MESSAGE);
		}
		
		 else		   //dispenser is empty
				JOptionPane.showMessageDialog(null,"Sorry "
									   + item
									   + " is sold out\n" +
									   "Make another selection",
									   "Thank you, Come again!",
									   JOptionPane.PLAIN_MESSAGE);
		
		
	}



this is what i put in the panel
exit.addActionListener(new ButtonHandler());
candy.addActionListener(new ButtonHandler());


	
	
	add (devices);

	}
	//to handle the buttons
	private class ButtonHandler implements ActionListener
	{ 
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource()==exit)
				System.exit(0);
		/*	else if (e.getSource()==candy)
			selection(candy,"Candy");*/
		else if (e.getActionCommand().equals("Chips"))
				selection(chip,"Chips");



how do i pass what the selection needs....
Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: how do i add the action listener

Posted 01 March 2009 - 12:36 PM

Quote

but how do i pass the parameters to my selection method in the main.


When you call the method just put the parameters in the parenthesis after the method name in the order they appear in the method signature.
Was This Post Helpful? 0
  • +
  • -

#5 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Re: how do i add the action listener

Posted 01 March 2009 - 01:12 PM

View PostBigAnt, on 1 Mar, 2009 - 11:36 AM, said:

Quote

but how do i pass the parameters to my selection method in the main.


When you call the method just put the parameters in the parenthesis after the method name in the order they appear in the method signature.


How do i get the panelviewclass to define this method.
its not recognizing it....
Was This Post Helpful? 0
  • +
  • -

#6 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: how do i add the action listener

Posted 01 March 2009 - 01:28 PM

Where is your panel class you are creating? If the selection method is private and in a separate panel class it cannot be called from any other class.
Was This Post Helpful? 0
  • +
  • -

#7 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Re: how do i add the action listener

Posted 01 March 2009 - 01:36 PM

the vendingmachine class has my main and also the selection method which is private void.
The panelview class has my panel and action handlers .
I am lost the teacher wanted me to build the panel class which i did now how do connect them together.
Was This Post Helpful? 0
  • +
  • -

#8 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: how do i add the action listener

Posted 01 March 2009 - 01:49 PM

If the selection method is private and the vending machine class it cannot be called from the panel class.

Post your panel class so as to see what it is doing and how it is calling the methods and such.
Was This Post Helpful? 0
  • +
  • -

#9 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Re: how do i add the action listener

Posted 01 March 2009 - 02:04 PM

View PostBigAnt, on 1 Mar, 2009 - 12:49 PM, said:

If the selection method is private and the vending machine class it cannot be called from the panel class.

Post your panel class so as to see what it is doing and how it is calling the methods and such.

okay heres my panelview class:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Panelview  extends JPanel {
	// create hello and goodbye buttons
	
	JFrame frame =new JFrame();
	
	//labels
	JLabel title = new JLabel ("Welcome to Dee's Vending Machine");
	JLabel Welcome= new JLabel ("Please make a selection");
	
	//buttons
	JButton candy = new JButton("Candy");
	JButton chip= new JButton("Chips");
	JButton pop = new JButton("Pop");
	JButton gum = new JButton("Gum");
	JButton exit = new JButton("Exit");

	JPanel devices = new JPanel(); 
	Panelview(){
	
	devices.setLayout(new GridLayout (7,1));
	
	devices.add(title);
	
	devices.add(candy);
	devices.add(chip);
	devices.add(pop);
	devices.add(gum);
	devices.add(exit);
	devices.add(Welcome);
	
exit.addActionListener(new ButtonHandler());
candy.addActionListener(new ButtonHandler());
chip.addActionListener(new ButtonHandler());
pop.addActionListener(new ButtonHandler());
gum.addActionListener(new ButtonHandler());
	add (devices);

	}
	//to handle the buttons
	private class ButtonHandler implements ActionListener
	{ 
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource()==exit)
				System.exit(0);
			else if (e.getSource()==candy)
			selection(candy,"Candy");
		else if (e.getActionCommand().equals("Chips"))
				selection(chip,"Chips");
			else if (e.getActionCommand().equals("Pop"))
				selection (pop,"Pop");
			else if (e.getActionCommand().equals("Gum"))
					selection (gum,"Gum");
			
			
		}	
			
		}

}//end class



this is really making me crazy...i
Was This Post Helpful? 0
  • +
  • -

#10 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: how do i add the action listener

Posted 01 March 2009 - 02:25 PM

Well the way you are doing it with the selection method

Quote

selection (gum,"Gum");

cannot be done like this because the selection method is in another class, and since it is private it cannot be accessed externally for any reason.

To do this you will have to either pass in a reference to the vending machine(and make the selection method public)
Or rework how you do the listeners to make it work the way you are doing it.

Also why do you create a JFrame inside of your JPanel?
Was This Post Helpful? 0
  • +
  • -

#11 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Re: how do i add the action listener

Posted 01 March 2009 - 02:35 PM

i have been editing and rediting not erasing al my code. Forgot about the frame.
is there a way to make the handler a function that returns the button choice then pass that return to the method selection.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1