• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array with Buttons"  RSS feed

 
Marcel Sam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello evryone, and i thank God for finding this java forum , anyway, as i am still new here; i deserve to ask a little question ; i am trying to make a program that has arrays with buttons in it; what i mean is that for each button (4 buttons); that when you press it; it should show a text associated with tha button; i started with the code and made the buttons but i don't know how to make the buttons make any action; , i am still trying but here is the codes below and i hope someone here can show me the light

Thank you evryone
Marcel

Also, here is a second try i tried; but still; buttons with no actions, here is the codes:

THANK YOU

[ May 02, 2005: Message edited by: Marcel Sam ]
[ May 02, 2005: Message edited by: Marcel Sam ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither of your attempts has an array of Button objects. Is this required? You may need to fix this if it is. Also, your actionPerformed() method has a couple of problems. First of all, ActionEvent.getSource() returns the Object that caused the event. In this case, a Button causes the event, not a String. This means that your if statements should look something like this instead of what you have:

I don't know if that will fix all of your problems, but at least its a start.

Layne
 
Marcel Sam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Layne for your reply; the Array is not required, i think it was just a wrong title/topic by me ; thank you for your help, i will try to make it work.
still though, any new advice is welcome.
thank you
Marcel
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Like Layne said above the event.getSource() returns an object and not a a String so you cannot compare it with a String.
But if you want to use the Label of the button then access it by doing the following:
Object source = event.getSource();
Button button = (Button)source;
String label = button.getLabel();

Then you can compare the label to the labels of the other buttons:

if(label.equals(("UB40"))
{//Your code
}
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
personally i never feel good about using the label on a button to determine which button it is. after all, labels can change; what if you have to translate your program to some other language? or even just find out your label's misspelled...?

JButtons have a thing called "actionCommand", though. it's a string (i think it's always a string; perhaps it could be any Object, for all i know) that you can set on the button with .setActionCommand() and then read off the button with .getActionCommand() when you need it. the actionCommand is never shown on the screen with the button, so there's no reason you should need to ever change it; you can use it as a way to uniquely identify the button itself, as opposed to what the button should say to the user.

the only problem is, i'm not entirely sure if this is how the actionCommand was meant to be used. the method works, but i'm not sure if it would confuse other people more used to Swing code doing it differently. any Swing gurus care to enlighten us?
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how would the actionCommand method help in the foreign language case? It certainly won't help with the typo scenerio (not much does). Just curious.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if, in your actionPerformed method (or similar) you use the actionCommand to determine which button was pressed, then it won't matter any longer what language the button's label is in — that can be allowed to change, so long as you keep the actionCommand the same. the actionCommand is never visible to the user, so there's no reason to change it. using the label on the button itself to tell buttons apart is not so robust.
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes. Now I get it. I don't work with awt or swing much(most everything is jsp for the view). Thanks for enlightening me
[ May 04, 2005: Message edited by: Hentay Duke ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hentay Duke:
So how would the actionCommand method help in the foreign language case? It certainly won't help with the typo scenerio (not much does). Just curious.


Since the actionCommand is not displayed it does not need to be translated, so internationalization is not an issue. To avoid problems with typos, you should still centralize the Strings used for actionCommands in some manner. For example, they can all be static final Strings (probably private, but it depends on which classes need to access them). Then you use one of these Strings to set the actionCommand() and you use it for comparison later. This way if you make a typo, it won't matter because it's only in a single location.

Personally, I prefer to create separate ActionListeners for each button. I also use anonymous inner classes for this purpose because I don't like coming up with names for each new ActionListener. The syntax seems a little strange at first, but you get used to it eventually. Here's an example to illustrate what I mean:

Using this approach, you no FOR SURE which button was clicked. There is no need for an if statement to figure out who caused the event.

I hope this helps. Let us know if you have any more questions.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!