aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes basic things with awt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "basic things with awt" Watch "basic things with awt" New topic
Author

basic things with awt

Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

I want to make something similar to a phone:
i want to have a display, and 10 buttons: 0-9 and enter button.
each time a press a button i want to display it on the screen(a TextField).
I`m thinking doing something like:


but i cannot apply switch to a Button array, i need to apply it to an int...any ideas?

thank you.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

I dont think you need to create an button array at all.
With the ActionEvent object you get on every single click event, you can call the getSource() method to identify which button object was exactly clicked and get the label out of that button.


OCPJP 6
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

There are so many things I could say about that code that I don't know where to start. For one, you've already discovered that you can't switch on a Button array. You can't switch on an int array either, so this shows you might need to go through a tutorial or two.
http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Note that the tutorial has already been updated to include switching on a String, which is a new feature of Java 7 and doesn't exist in Java 6.

Then too, creating any kind of array in the actionPerformed most certainly isn't the way to go. Perhaps these tutorials may help.
http://download.oracle.com/javase/tutorial/uiswing/components/button.html
http://download.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Finally, I would recommend using Swing rather than AWT. You get all the functionality of AWT and more, and good online tutorials too.


luck, db
There are no new questions, but there may be new answers.
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

I got it!
Thanks for the help:



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
That is nothing like object-oriented programming. Create a Listener class, which you have already tried, which takes the details from the button for its field
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Also, you can even simplify your ActionListener implementation..Something like....
Checking the action command for every single button in a if-elseif-else might look a bit redundant to people.

This is the basic idea(and note - I have not tested the above code...).
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Thanks for the replies. I`ll submit my code and then i`ll try to modify it





UPDATE CODE
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

I`m looking to implement a listener for the Enter button, when someone enters they're four digit ID, to do a search in a text file and retrieve the customers first name, second name and the account. I've tought of something but i''ll work on it later and let you know.

Thank again for all the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: basic things with awt