Dalbenn
Dalbenn

Reputation: 127

Java applet: actionlistener of button with noname

I have a HashMap of JButtons which represents pictures.

this.nahledy.put(s.getName(), new JButton());

this.nahledy.get(s.getName()).addActionListener();

but I don't know how to tell the actionlistener which button has been pressed since the e.getActioncommand() is nothing and the s.getName() is already changed when user presses the button so I can't use it too.

Upvotes: 1

Views: 474

Answers (1)

jobnz
jobnz

Reputation: 398

Its not perfect but maybe you can use something like this

public void actionPerformed(ActionEvent e) {
   JButton source = (JButton)e.getSource();
   Set<String> keys = nahledy.keySet();

   for(String key : keys) {
      if(nahledy.get(key).equals(source)) {
         // do something with your button
      }
   }
}

This way you can find out the value of s.getName() you used for this button.

Upvotes: 1

Related Questions