Results 1 to 5 of 5
  1. #1
    snaquetime is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default Loop + inner class error

    Sorry the code isn't completely tidied, but just one small annoying bug in my way.

    Java Code:
    JButton[] newTasks = new JButton[7];
    ActionListener[] makeNew;
    int avalue = 0;
    
    private void addListeners() {
            for(int y = 0; y<7; y++) {
                avalue=y;
                makeNew[avalue] = new ActionListener() {
                    public void actionPerformed(ActionEvent actionevent) {
                    System.out.println(newTasks[avalue].getParent().getParent().getName());
                    }
                };
                newTasks[y].addActionListener(makeNew[avalue]);
            }
        }
    So each actionlistener in the actionlistener array is added to the corresponding button, or so I wish it to be. In truth, the last actionlistener (the 7th one in the example I gave as a placeholder) seems to be connected to all of the buttons. Perhaps I misunderstand the concept of an actionlistener or inner classes. Anyone have ideas?
    Last edited by snaquetime; 08-30-2011 at 04:34 AM.

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

    Default

    Java Code:
    System.out.println(newTasks[avalue].getParent().getParent().getName());
    Your diagnosis of the same actionListener being attached to all buttons is incorrect. At the end of all your coding to create and add listeners the avalue variable will be 6. You will have created 7 actionListeners but each of them will execute the same code (as above) and since for all of them avalue has the same value it seems it will be doing the same thing. Looks like you need to rethink your design.

  3. #3
    snaquetime is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    as far as I can tell, the loop changes the y variable, which changes avalue, which means the arrays listed have their index changed for each repeat of the loop, so how do all of the actionlisteners have the same avalue? The line that you posted is specific to my project and doesn't seem to have much relevance out of context, but just shows that each button should return a different answer because they have different parents. Would really appreciate if you could clarify a bit more cuz I'm still a bit mystified. Thank you

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

    Default

    Java Code:
    System.out.println(newTasks[avalue].getParent().getParent().getName());
    The value of the variable changing while the loop is executing is irrelevant. What is important is what the value of the variable is when an actionListener is evoked. Once the loop ends the value of avalue is 6. It never changes after that. So when an actionListener executes the above line of code it will retrieve the button in the newTasks array with an index of 6. Each Listener will retrieve the same button from the array. Just because avalue was 0 when you created the first Listener is absolutely irrelevant. What is relevant is what value avalue has when the actionPerformed method is executed.

  5. #5
    snaquetime is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 03-27-2011, 06:49 AM
  2. Do While loop error
    By JeffMCincy in forum JCreator
    Replies: 1
    Last Post: 02-21-2011, 06:54 PM
  3. Loop error
    By Spyderpig in forum New To Java
    Replies: 4
    Last Post: 02-19-2011, 01:56 AM
  4. For loop error
    By ShotGunRockets in forum New To Java
    Replies: 6
    Last Post: 04-06-2009, 02:14 AM
  5. help error in while loop
    By iPetey in forum New To Java
    Replies: 3
    Last Post: 04-03-2009, 04:56 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
  •