0

I'm writing a program and I've run into an issue...

I create 1 JLabel array and 1 JButton array. The JLabel array holds a string, a Club Name. The JButton array holds a string that just says "Edit".

A For loop then fills each array based on the length of the clubs array, as well as add an action listener for each button.

When the user clicks the JButton corresponding to the JLabel it initiates an Event. In this event I need to find out the value stored in the JLabel that matches the JButton.

Since the event listener doesn't know it's inside a loop, I can't use it.

How do I achieve the goal I want?

See the bellow code.

JLabel clubs[]      = new JLabel[99];
JButton editAClub[] = new JButton[99];

for(int i=0; i <= (allClubs.length - 1);i++)
{
    clubs[i]        =   new JLabel("Club " + i);
    editAClub[i]    =   new JButton("Edit");
    editAClub[i].addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            selectedClub = clubs[i].getText().toString();
            System.out.println(selectedClub);
        }
    });
}   
2
  • 1
    what's wrong with the current code? Commented Apr 6, 2013 at 18:24
  • You can't use i inside the actionListener. Commented Apr 6, 2013 at 18:55

1 Answer 1

1

I would create a map of Buttons and JLabels and pass the source of the action in the actionListener:

JLabel clubs[]      = new JLabel[99];
JButton editAClub[] = new JButton[99];

//create a map to store the values
final HashMap<JButton,JLabel> labelMap = new HashMap<>(); //in JDK 1.7

for(int i=0; i <= (allClubs.length - 1); i++)
{
    clubs[i]        =   new JLabel("Club " + i);
    editAClub[i]    =   new JButton("Edit");

    //add the pair to the map
    labelMap.put(editAClub[i],clubs[i]);

    editAClub[i].addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            //get the label associated with this button from the map
            selectedClub = labelMap.get(e.getSource()).getText(); // the toString() is redundant
            System.out.println(selectedClub);
        }
    });
}   

This way the Buttons and Labels are associated with each other via a separate data structure rather than just by their indices in their respective arrays.

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

8 Comments

Valek, that throws NullPointerException's... I Also had to make the labelMap final. Either way though doesn't work..
Issue resolved, I used Valek's idea... However, rather then use "this" I used e.getSource().
@RickyRodrigues Fixed. Sorry, I get confused about the different levels of this within anonymous class declarations. feel free to accept if it solved your problem.
So why not use ((JButton)e.getSource())?
@MadProgrammer Why bother? get takes an Object as its parameter.
|

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.