Getting a method from one class to another

action listeners in one class. Want the action to be in another class

Page 1 of 1

5 Replies - 2076 Views - Last Post: 03 March 2009 - 05:15 PM Rate Topic: -----

#1 debeth  Icon User is offline

  • D.I.C Head

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

Getting a method from one class to another

Post icon  Posted 02 March 2009 - 01:22 PM

How do i use this button handler in my panel class.
To perform the method in my main class...No comprehension here.

this is my button handler method in my panel class:
	//to handle the buttons
	public 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 buttonhand 


and this is my method in another class
//method to  get item

	 public 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);
		
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Getting a method from one class to another

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

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

Re: Getting a method from one class to another

Posted 02 March 2009 - 01:28 PM

You will have to pass a reference of your class containing the selection method to the handler class in order to access the non static method selection, you can store this in a class field when you create an instance, and pass in as a parameter to the constructor
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: Getting a method from one class to another

Posted 02 March 2009 - 01:34 PM

create an instance of the selection method like this

selection( string);

This is what i mean about no comprehension.
Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

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

Re: Getting a method from one class to another

Posted 02 March 2009 - 02:48 PM

Quote

selection( string);


The selection method takes 2 arguments, so what are you trying to accomplish with this, have you tried passing in the reference to the constructor?
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: Getting a method from one class to another

Posted 03 March 2009 - 10:57 AM

View PostBigAnt, on 2 Mar, 2009 - 01:48 PM, said:

Quote

selection( string);


The selection method takes 2 arguments, so what are you trying to accomplish with this, have you tried passing in the reference to the constructor?


okay i redid it again put the Action listener in the main class.
Well its not working!!!!!
Any clues
import javax.swing.*; 
import javax.swing.JOptionPane;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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);
 
	
	//buttons handlers
	private ButtonHandler bHandler;
	private JButton candyB,chipB,gumB,popB;

	public vendingmachine()
	{
		//listner object instance
		
	bHandler=new ButtonHandler();
	
	//instantiate the buttons
	candyB=new JButton("Candy");
	candyB.addActionListener(bHandler);
	
	chipB=new JButton ("Chips");
	chipB.addActionListener(bHandler);
	
	gumB=new JButton("Gum");
	gumB.addActionListener(bHandler);
	
	popB=new JButton("POP");
	popB.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 buttonhand

	//method to  get item

	 public 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


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Getting a method from one class to another

Posted 03 March 2009 - 05:15 PM

On pass the instance of the class to call back in the listener constructor

class MyActionListener implement ActionListener {
	 vendingmachine callBack;

	 MyActionListener(vendingMachine vm) {
		callBack = vm;
	 }

	 public void actionPerformed(ActionEvent e) {
		  callBack.whatEverMethod();	  // call back the method in vendingmachine
	 }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1