• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Get selected object name or index

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic