• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to get user input?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a program that includes a menu and will do something different for each menu choice, but I am stuck on how to get the menu selection from the user...
Thanks in advance!
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll have to post a more detailed question in order to get good replies.

What kind of program? GUI, command line?

I'm assuming it's a GUI program. Have you written the GUI yet? Have you implemented the MenuBar?

There are some good tutorials out there on how to implement MenuBars in a Java GUI.
 
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bowe,
If you are using a menu which has menu items (menu = file, menu item= new) you can add the action listener to the menu items and in the actionPerformed() you can check which menu item was clicked and do what you want to do. Hope this helps.
 
M.L. Bowe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is actually an application that has a menu (no GUI). That is my big problem - I have the application so that it prints out the menu, but now I need the user selection to tell the program what to do from there.
Thanks.
 
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whht you will want to do is read from System.in in order to do this. It is an InputStream, so check out the java doc for that.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing we all found disappointing the first time is that you can't get a single keystroke menu choice from the user. You have to make the user press Enter to get anything. Reading from the console is just annoyingly fussy enough that you might want to make a little utility class to hold some reusable code. By way of example, think about how you'd make this work:

If you're in JDK 5 see if you can use Scanner instead of BufferedReader. And let me know the answer!
 
You showed up just in time for the waffles! And this 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