0

Is there any way to set up a loop to create multiple very similar action-listeners? For purely academic purposes, I created a card game, each card is a button, when you click on the card, it should turn face-up. The game works fine, but each action listener is set up individually. Below is my attempt at a loop.

String string1;
String string2;

// called by the constructor
public void setupActionListener() {
    int loopVar = 0;

    while (loopVar <= 16) {
        string1 = "card"+loopVar;
        string2 = "c"+loopVar;

             string1 string2 = new string1();           //A
             // -> card0 c0 = new card0();

             card[loopVar].addActionListener(string2);  //B
             // -> card[0].addActionListener(c0);

    loopVar = loopVar + 1;
}


// the action listener

public class card0 implements ActionListener {          //C
    public void actionPerformed(ActionEvent c0) {       //D
        // perform actions
    }  
}

EDIT: First, I never thought "string1 string2 = new string1();" would work, I was just going for a general idea.

Judging from the comments, it seems I have asked this question in a confusing way, all I meant is I have this in the constructor:

card0 c0 = new card0();
card[0].addActionListener(c0); 
card1 c1 = new card1();
card[1].addActionListener(c1); 
card2 c2 = new card2();
card[2].addActionListener(c2);
card3 c3 = new card3();
card[3].addActionListener(c3);
card4 c4 = new card4();
...     
card15 c15 = new card15();
card[15].addActionListener(c15);

And later this:

public class card0 implements ActionListener {
    public void actionPerformed(ActionEvent c0) {
        move(0);
    }  
}

public class card1 implements ActionListener {
    public void actionPerformed(ActionEvent c1) {
        move(1);
    }  
}

public class card15 implements ActionListener {
    public void actionPerformed(ActionEvent c2) {
        move(2);
    }  
}
...
public class card15 implements ActionListener {
    public void actionPerformed(ActionEvent c15) {
        move(15);
    }  
}

And I wanted to know if it was possible to put all this into a loop. It seems the answer is no, but I was just asking.

8
  • 1
    What do you think string1 string2 = new string1(); should do? Commented Aug 19, 2014 at 23:04
  • I would rather create one instance of the listener and have its logic find which button raised the event. Commented Aug 19, 2014 at 23:04
  • You should start with some java tutorial because judging from this: string1 string2 = new string1() there's a lot to explain here. Try finding some tutorial like: docs.oracle.com/javase/tutorial or learnjavaonline.org Commented Aug 19, 2014 at 23:08
  • You are trying to create a class card, where? Commented Aug 19, 2014 at 23:11
  • Start by taking a look at How to Write an Action Listeners. I don't know what card is, but most components that support the ActionListener API require an instance of an ActionListener when registering... Commented Aug 20, 2014 at 0:04

1 Answer 1

3

The code you presented is not very rich so I'm not sure if I understand you correctly, but maybe this pseudo-code would help you.

public class Card extends SomeComponent{
    public void turn() {
        //doStuff
    }
}

public class CardGame {

    private SomeComponent[] cards;
    public void prepareGame() {
        CardListener listener = new CardListener();
        for (int i=0; i<=16; i++) {
            cards[i] = new Card();
            cards[i].addActionListener(listener);
        }
    }
}

public class CardListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        ((Card) e.getSource()).turn();
    }

}

You can use adding MouseListener or KeyListener instead of ActionListener which doesn't give you much information about what has happened.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.