• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

basic things with awt

 
Ranch Hand
Posts: 36
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it!
Thanks for the help:



 
Marshal
Posts: 80226
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic