Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes array of buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "array of buttons" Watch "array of buttons" New topic
Author

array of buttons

aymane chetibi
Ranch Hand

Joined: Apr 12, 2006
Posts: 175
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

Joined: Aug 30, 2005
Posts: 407
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

Joined: Apr 12, 2006
Posts: 175
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

Joined: Jun 09, 2003
Posts: 4632
you could use
button.getActionCommand()

if you don't use setActionCommand(..), it defaults to the button's text.
aymane chetibi
Ranch Hand

Joined: Apr 12, 2006
Posts: 175
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Apr 12, 2006
Posts: 175
thanks a lot I figured that out.

thanks again.
regards,
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array of buttons
 
Similar Threads
How to get eventSource ??
build a data entry sheet like spread-sheet
JDBC Suggestion Needed
Is it possible to remove slider from JScrollbar
model for group of radio buttons