• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

basic things with awt

 
Mihai Lita
Ranch Hand
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mihai Lita
Ranch Hand
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it!
Thanks for the help:



 
Campbell Ritchie
Sheriff
Pie
Posts: 49380
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. I`ll submit my code and then i`ll try to modify it





UPDATE CODE
 
Mihai Lita
Ranch Hand
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic