Results 1 to 3 of 3
  1. #1
    Kevinw778 is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Action-Listener with multiple Arguments? (Button Listener, specifically)

    I'm attempting to create a buttonListener for a method with multiple arguments which are simply ints.. I understand that the arguments being passed into the Listener will have to be Objects (Integers), however I'm not entirely sure how to go about this.

    This is the method I am attempting to create the button listener for:

    Java Code:
    public void startUpModel(int numberOfQueues, int serviceDelay, int generationInterval, int amountOfCustomers)
    	{
    		amountOfQueues = numberOfQueues;
    		myServiceQueueManager = new ServiceQueueManager(amountOfQueues);
    		myCustomerGenerator = new UniformCustomerGenerator(generationInterval,amountOfCustomers,myServiceQueueManager);
    		myUniformCashiers = new UniformCashier[amountOfQueues];
    		for(int i=0; i<amountOfQueues; i++)
    		{
    			ServiceQueue iterationServiceQueue = myServiceQueueManager.getServiceQueues()[i];
    			myUniformCashiers[i] = new UniformCashier(serviceDelay, iterationServiceQueue);
    		}
    	}

    The following is the "associateListenerS()" method I have attempting to setup the buttonLIstener for the above method. As I said I'm not sure how to have it so all the parameters can be passed in and used in the buttonListener.

    Java Code:
    public void associateListeners()
        {
            String error;
            Class<? extends Controller> controllerClass;
            Method incrementMethod,
                   decrementMethod;
            Class<?>[] classArgs;
            Integer[] args;
            
            controllerClass = myController.getClass();
            
            error = null;
            incrementMethod = null;
            decrementMethod = null;
    
            classArgs = new Class[1];
            args = new Integer[1];
            
            // Set argument types for method invokations (the "Russin'" way to spell it ;)
            try
            {
               classArgs[0] = Class.forName("java.lang.Integer");
            }
            catch(ClassNotFoundException exception)
            {
               error = exception.toString();
               System.out.println(error);
            }
            
            // Associate method names with actual methods to be invoked
            try
            {
               incrementMethod = controllerClass.getMethod("increment",classArgs);
               decrementMethod = controllerClass.getMethod("decrement",classArgs);        
            }
                
            catch(NoSuchMethodException exception)
            {
               error = exception.toString();
               System.out.println(error);
            }
            catch(SecurityException exception)
            {
               error = exception.toString();
               System.out.println(error);
            }
            
            // Set up listeners with actual argument values passed in
            // to methods
            
            args[0] = new Integer(20);
            myIncrementListener 
                   = new ButtonListener(myController, incrementMethod, args);
            myDecrementListener 
                   = new ButtonListener(myController, decrementMethod, args);
        }
    The following is the error I get when trying to run my program with what I have currently set-up for the buttonListener:
    Java Code:
    java.lang.IllegalArgumentException: wrong number of arguments
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at gui.ButtonListener.mouseReleased(ButtonListener.java:59)
    (There is more but I looked through it and I don't believe all of it is entirely relevant.

    Any help would be appreciated, thanks!

    Bank.zip

    This is all of my code attached, look at it if it'll help you understand more thoroughly.
    The method in question was in the "Controller" class. The associateListeners was in the "View" class.
    Much appreciated!

  2. #2
    Kevinw778 is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Re: Action-Listener with multiple Arguments? (Button Listener, specifically)

    **Note, the associatListeners(Controller con) method that I put in is the wrong one from an example that I was looking at.
    The correct one is in the "Gui" class.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Action-Listener with multiple Arguments? (Button Listener, specifically)

    arguments being passed into the Listener will have to be Objects (Integers), however I'm not entirely sure how to go about
    What method is the Listener?
    IllegalArgumentException: wrong number of arguments
    ..
    at java.lang.reflect.Method.invoke(Unknown Source)
    at gui.ButtonListener.mouseReleased(ButtonListener.ja va:59)
    Where is the code for ButtonListener? Especially line 59

Similar Threads

  1. Help with Combo box and action listener
    By sunilmenhdiratta in forum AWT / Swing
    Replies: 2
    Last Post: 06-25-2011, 09:32 PM
  2. Replies: 5
    Last Post: 03-02-2011, 10:45 PM
  3. Problem in action listener
    By cool in forum AWT / Swing
    Replies: 4
    Last Post: 11-16-2010, 08:44 AM
  4. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 06:39 PM
  5. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 09:51 AM

Tags for this Thread

Posting Permissions

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