• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get selected object name or index  RSS feed

 
dushantha Rathnayake
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Think, I have a JButton array. Using for loop I'm initializing them, set their bounds etc. My problem is, I'm giving ActionListener inside of this loop. If any user click any button I want to do some actions but the action is depends on the button. So how can I detect witch button is pressed by user ?. Actually I want to get the buttons index.

Thank you.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) loop through the array to check if the
source is equal to the button at that index

or
2) buttons have a name property. set it
during the array construction loop as the
index number of the button (will be a string).
when the button is clicked, you 'get' the name
from the source, parsing 'name' to int for
the index number
 
dushantha Rathnayake
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you Michale for the reply. I found a method.



In initializing step I set the JButton tooltiptext as my coordination (x,y) so after button pressed I can get the tooltiptext.

Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!