Results 1 to 17 of 17
  1. #1
    JGBowers is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default ActionListener Calling methods from a different class

    Hello,

    I am trying to have some ActionListener-inner classes to call methods from a different class. I am new to this site so I apologize if anything I do is out of the normal ways of doing things.

    My action listeners:

    Java Code:
       public class addCoin implements ActionListener
       {
          private int dep;
          
          public addCoin(int amount)
          {
             dep = amount;
          }
          public void actionPerformed(ActionEvent event)
          {
             //deposit(dep)
          }
       }
    //SODA ACTION LISTENER
    Java Code:
       public class Soda implements ActionListener
       {
          private String soda;
          private int ndx;
          
          public Soda(String name, int index)
          {
             soda = name;
             ndx = index;
          }
          
          public void actionPerformed(ActionEvent event)
          {
             //soda buttons disbaled
             //coin return disabled
             //change return field = amount deposited - cost
             //deposited = 0
             //totals sales += cost
          }
       }

    //COIN RETURN ACTION LISTENER
    Java Code:
       public class Return implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {
             //disable sodas
             //coin return disabled
             //change return = deposit amount
             //amount deposited = 0
          }
       }


    The methods I am trying to use are:
    Java Code:
    public class SodaMachine implements Serializable
    {
       private static final int SODA_COST = 85;
       private ArrayList<Soda> mSodas;
       private int mDeposits;
       private int mSales;
    
       public SodaMachine()
       {
          mSodas = new ArrayList<Soda>();
    
          mSodas.add(new Soda("Jolt", 1));
          mSodas.add(new Soda("Squirt", 2));
          mSodas.add(new Soda("Mountain Dew", 3));
          mSodas.add(new Soda("A&W Rootbeer", 4));
          mSodas.add(new Soda("Pepsi", 3));
          mSodas.add(new Soda("Dr. Pepper", 2));
          mSodas.add(new Soda("7-Up", 1));
       }
    
       public int dispenseSoda(int index)
       {
          if(!sufficientFunds())
          {
             throw new InsufficientFundsException("Deposits of " + mDeposits + " is less than cost of " + SODA_COST);
          }
    
          ((Soda)mSodas.get(index)).dispense();
    
          mSales += SODA_COST;
          int change = mDeposits - SODA_COST;
          mDeposits = 0;
    
          return change;
       }
    
       public boolean sufficientFunds()
       {
          if(mDeposits >= SODA_COST)
          {
             return true;
          }
    
          return false;
       }
    
       public int getCount()
       {
          return mSodas.size();
       }
    
       public String getSodaName(int index)
       {
          return ((Soda)mSodas.get(index)).getName();
       }
    
       public int getSodaCount(int index)
       {
          return ((Soda)mSodas.get(index)).getCount();
       }
    
       public void deposit(int amountInCents)
       {
          mDeposits += amountInCents;
       }
    
       public int returnDeposits()
       {
          int change = mDeposits;
          mDeposits = 0;
          return change;
       }
    
       public int getSales()
       {
          return mSales;
       }


    Since the total amounts need to be synchronized between all action listeners I don't believe constructing a SodaMachine in each one would work.

    This is a school project and I'm not look for exact code answers, but simply the basic idea on how to call out-of-class methods from ActionListener classes.

    Thanks for any help.
    Last edited by Fubarable; 11-25-2011 at 05:17 AM. Reason: code tags added

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: ActionListener Calling methods from a different class

    An ActionListener is an object which you attach to a button or some other component in a GUI. Where is your GUI? If you are not doing a GUI then the use of ActionListeners is the wrong way to go.

  3. #3
    JGBowers is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: ActionListener Calling methods from a different class

    I didn't include it because it was a considerably larger amount of code. I can if it would make answering the question easier?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ActionListener Calling methods from a different class

    It looks as if each ActionListener class needs a reference to the model class -- SodaMachine. So I'd either pass a reference to this class as a parameter in each ActionListener's constructor or as part of a setter or mutator method. Then the ActionListener can call the model's methods as needed. The ActionListeners (the control) will also need a reference to the GUI or view class as well.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: ActionListener Calling methods from a different class

    Ok your GUI code is elsewhere. Even so looking at what you have posted making your Soda class implement ActionListener still seems wrong to me.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ActionListener Calling methods from a different class

    Quote Originally Posted by Junky View Post
    Ok your GUI code is elsewhere. Even so looking at what you have posted making your Soda class implement ActionListener still seems wrong to me.
    Agree. Soda should be part of the Model classes since the SodaMachine holds Soda objects.

  7. #7
    JGBowers is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: ActionListener Calling methods from a different class

    Wouldn't passing a reference of SodaMachine to the action listeners make the nubmers different?

    So deposit called from one of them would hold a different answer than the other ones.

    And I'm not sure how the actionlisteners appear wrong. I am making a soda machine that has buttons for coins (nickels/dime/quarter etc) and types of soda and each button raises or lowers the amount deposited in the machine and/or the total sales of the machine.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: ActionListener Calling methods from a different class

    Quote Originally Posted by JGBowers View Post
    Wouldn't passing a reference of SodaMachine to the action listeners make the nubmers different?
    What numbers?
    So deposit called from one of them would hold a different answer than the other ones.
    I don't understand what you are saying/asking.
    And I'm not sure how the actionlisteners appear wrong. I am making a soda machine that has buttons for coins (nickels/dime/quarter etc) and types of soda and each button raises or lowers the amount deposited in the machine and/or the total sales of the machine.
    But you attach the ActionListeners to the buttons and not the Soda cans.
    Java Code:
    JButton cokeButton = new JButton();
    cokeButton.addActionListener(new ActionListener() {
        public void actionPerformed() {
            dispenseCoke();
        }
    });
    Something along those lines. The dispense method would do whatever is needed to dispense a coke Soda.

  9. #9
    JGBowers is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: ActionListener Calling methods from a different class

    A sodamachine reference would hold it's own instance variables and when a method from sodamachine was called it would change that references variables wouldn't it?

    I need all actionlisteners to be synchronized with the variables of the SodaMachine reference.

    Not sure if that makes sense how I worded it. I know what I'm saying but I don't know if I'm explaining it well haha.



    Your code excerpt is what I'm trying to do and I understand it, but the action listener has to call the methods from the sodamachine class.
    As I stated in my original post this is a school assignment, I am only allowed to fiddle with the file that has the actionlisteners in it, and they must use the original methods from the other class.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: ActionListener Calling methods from a different class

    Well you are going to need a reference to a SodaMachine instance.
    Java Code:
    SodaMachine machine = new SodaMachine();
    JButton cokeButton = new JButton();
    cokeButton.addActionListener(new ActionListener() {
        public void actionPerformed() {
            machine.dispenseCoke();
        }
    });

  11. #11
    JGBowers is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: ActionListener Calling methods from a different class

    But if I have 3 actionlistener classes and each gets a reference to my SodaMachine object they won't hold the same instance variables. So I start with one deposit and then when I change the deposit with an actionlistener class it will have a new deposit.

    So start with deposit: deposit

    Change deposit in listener: deposit,A

    Change deposit with new listener: deposit,B

    etc.

    Not sure if that's making sense.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: ActionListener Calling methods from a different class

    You have not been listening. I'm saying you should not have those ActionListener classes or classes that implement ActionListener. Soda IS NOT an ActionListener.

  13. #13
    JGBowers is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: ActionListener Calling methods from a different class

    Then how do I make my buttons on my soda machine do something? I believe you also have not been listening because I have specifically said I have to do it this way as per the instructions given for this assignment.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: ActionListener Calling methods from a different class

    I have been listening, I have even posted you some code which may not be perfect but it gives you an idea of what you need to do. Adapt it to your needs. You should have a GUI class, you should have a SodaMachine class which holds N cans of Soda. You create an instance of the SodaMachine class in the GUI class (or maybe you have some other class acting as a Controller which creates the SodaMachine and GUI). Either way you need a reference to the SodaMachine in your GUI class. Then using the code I posted when the cokeButton is clicked it fires off the ActionListener which calls the dispense method in the SodaMachine class. Think long and hard about what I have said. Look at your code. It will need some changing to effect what I have described.

  15. #15
    JGBowers is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: ActionListener Calling methods from a different class

    As I have said a few times that goes against the parameters of the assignments, making me believe you have not been listening. Perhaps you are just explaining it in a way I do not understand.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: ActionListener Calling methods from a different class

    For starters we have no idea what your assignment does or does not say. Secondly are you sure you understand your assignment? If not seek clarification from your teacher. If your assignment does say to make your Soda class implement ActionListener I'd be very surprised as that is definately the wrong way to do it.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ActionListener Calling methods from a different class

    I agree that your Soda class should not be an AbstractAction or ActionListener class, but perhaps you should create a "BuySoda" class to represent the button pressed when someone wants to buy a soda.

    Regarding questions:
    But if I have 3 actionlistener classes and each gets a reference to my SodaMachine object they won't hold the same instance variables. So I start with one deposit and then when I change the deposit with an actionlistener class it will have a new deposit.

    So start with deposit: deposit

    Change deposit in listener: deposit,A

    Change deposit with new listener: deposit,B

    etc.
    Each listener will have different variables, but the model variables in each listener will all refer to the same Model object. So changes in one listener will all affect the same ultimate object.

Similar Threads

  1. Calling base class methods
    By new2java2009 in forum New To Java
    Replies: 1
    Last Post: 11-15-2011, 10:47 PM
  2. Calling methods into another class? Help please!
    By javanator in forum New To Java
    Replies: 1
    Last Post: 03-20-2010, 06:19 PM
  3. Calling for methods
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 02-27-2010, 09:12 PM
  4. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum Advanced Java
    Replies: 3
    Last Post: 01-24-2009, 12:58 PM
  5. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 05:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •