Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can i get the index of which button was clicked?

 
Nitesh Panchal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Say suppose i created a Button array now when i click upon it the control goes to actionPerformed(ActionEvent e) method. Now i know that you get the source as well the string related to the button via getSource() and getActionCommand() respectively. But can i get the index of the button like which button was clicked? Thanks in advance!
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to iterate through the array and compare the event source to the button references you've stored in it.

Or you can make the index part of the command string, and extract it using string operations.
 
Nitesh Panchal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic