File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array of buttons

 
aymane chetibi
Ranch Hand
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • 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,
 
jay vas
Ranch Hand
Posts: 407
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot I figured that out.

thanks again.
regards,
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic