This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 74757
336
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic