• 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

array of buttons

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

I have an application in which the number of buttons to display is known at run time.
I store them in an array of buttons.

How can I handle the actions of these buttons, and know which one has issued which action so that I know what to do next.

Please help me with that, and if you think the array solution is wrong please give me suggestions.

Thanks a lot.
regards,
 
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I handle the actions of these button ?

add ActionListeners to them. When a button is clicked, the code
in youre action listener will be ran.

and know which one has issued which action so that I know what to do next?

That should be handled in the Model component of your class. Are you familiar with the basics of MVC ?
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know MVC, can you just tell me what is the code I should put to listen to the action of those buttons ?
and then how to know wgicg one it is:
if( (JButton)e.getSource().getText == "blabla")

something like that .

thanks
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you could use
button.getActionCommand()

if you don't use setActionCommand(..), it defaults to the button's text.
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I am not explaining well what the problem is.

the problem is again how to listen for the action of those buttons, and once we get an action how to determine which button is it.
maybe your last reply answers the second question but where can I check this value ?

regards,
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in a simple case (not that you'd actually do it this way),
consider the buttons on a calculator

if(button.getActionCommand().equals("1"))//id's the button displaying the "1"
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot I figured that out.

thanks again.
regards,
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Typically, I wouldn't use action commands for that. Instead I would register a new listener to each button, so that each listener knows which button it is responsible for.
reply
    Bookmark Topic Watch Topic
  • New Topic