Help with Old School Menu

Help with Old School Menu

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1489 Views - Last Post: 20 February 2007 - 04:06 AM Rate Topic: -----

#1 codeninja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 16-February 07

Help with Old School Menu

Posted 16 February 2007 - 09:15 AM

Hi.

I am working with some studnets on various projects which they have been asked to complete to earn a certificate in high school level computer science. The structure of their program and curriculum are highly regulated by a 3rd party organization....like the SAT tests are.

During my first year in this program with them I followed the rules I was given too closely. They were not supposed to have to learn or know any code relating to GUI's. Their program is actually graded on paper! Although I must certify it before it leaves the school.

Anyway they need to make a menu that fires their main classes. The menu needs to look like this:

[terminal/console output]
-----------Menu-------------
1. option 1 (means press 1 on keyboard)
2. option 2
3. option 5
4. kill program.


The programs are all going ok -- simple database things based on xml.

But the only experience I have with Java is limited and limited to Guis. Because I did not teach the GUI -- i messed up thinking it would be easy to find and study old-school menu examples.

They get no points for any gui or graphic elements.

Anyway if anyone can direct me to a tutorial --or if you have a simple file I can start with I would appreciate it.

I should have lots of files to contribute as well. I will keep posting around.

CN

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Old School Menu

#2 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: Help with Old School Menu

Posted 16 February 2007 - 12:44 PM

I've read through your post a bunch of times, but I'm not sure that I really understand your problem.

Are you having trouble teaching how to build guis, or are you having trouble with the idea of creating a program where a user controls the flow of the program depending on their input?
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Help with Old School Menu

Posted 16 February 2007 - 03:01 PM

If I am understanding your question correctly, you want to know how to create an app that only uses a console interface instead of a GUI.

You can find everything you need at the following tutorials.
http://www.cs.wisc.e.../io/JavaIO.html
http://java.sun.com/...l/io/index.html
Was This Post Helpful? 0
  • +
  • -

#4 Ryan Smith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 14-February 07

Re: Help with Old School Menu

Posted 16 February 2007 - 03:02 PM

A simple way to get Standard Input from a keyboard is:

try {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String string = "";
		while (string != null) {
			System.out.print("> prompt ");
			string = in.readLine();
			doWhatever(string);
		}
	} catch (IOException e) {
	}


Run this simple class and see if it meets any of what you need to do:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) {
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			String string = "";
			System.out.println("Say something to me!");
		while (string != null) {
			System.out.print("> prompt ");
			string = in.readLine();
			doWhatever(string);
			}
		} catch (IOException e) {
		}
	}
	private static void doWhatever(String str) {
		if(str.equalsIgnoreCase("exit") || str.equalsIgnoreCase("quit") || str == null)
			System.exit(0);
		System.out.println("You say " + str + "?");
	}
}



You should be able to read the options from the doWhatever() method.

Hope that helps;
Was This Post Helpful? 0
  • +
  • -

#5 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: Help with Old School Menu

Posted 16 February 2007 - 07:44 PM

If that's really what you mean, taking input from the console, I've got a very easy solution for you.

Use the Scanner class.

import java.util.Scanner.*;

public class ReadConsole
{
  public static void main(String[] args)
  {
	Scanner input = new Scanner(System.in);
	String read = input.nextLine();
	Int num = input.nextInt();
   }
}



The Scanner class is very easy to use, and it prevents you from needing to understand all of the wrapping that goes on with the IO package.

Just create a new Scanner object like I did with the constructor above. Then, everytime you call input.nextLine() or input.nextInt(), the console will wait for you to input some data, which is immediately stored in the variable preceeding the call.

Here's the api in case you need to look something up:
http://java.sun.com/...il/Scanner.html

Enjoy.
Was This Post Helpful? 0
  • +
  • -

#6 Ryan Smith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 14-February 07

Re: Help with Old School Menu

Posted 16 February 2007 - 11:09 PM

Ha wow, I've never even used that...
Was This Post Helpful? 0
  • +
  • -

#7 codeninja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 16-February 07

Re: Help with Old School Menu

Posted 17 February 2007 - 11:31 AM

Wow! So much good feedback. I will let my kids try it out and I as well and then post our results!

Tony D aka codeninja the domination

View Postkeems21, on 16 Feb, 2007 - 07:44 PM, said:

If that's really what you mean, taking input from the console, I've got a very easy solution for you.

Use the Scanner class.

import java.util.Scanner.*;

public class ReadConsole
{
  public static void main(String[] args)
  {
	Scanner input = new Scanner(System.in);
	String read = input.nextLine();
	Int num = input.nextInt();
   }
}



The Scanner class is very easy to use, and it prevents you from needing to understand all of the wrapping that goes on with the IO package.

Just create a new Scanner object like I did with the constructor above. Then, everytime you call input.nextLine() or input.nextInt(), the console will wait for you to input some data, which is immediately stored in the variable preceeding the call.

Here's the api in case you need to look something up:
http://java.sun.com/...il/Scanner.html

Enjoy.

Was This Post Helpful? 0
  • +
  • -

#8 codeninja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 16-February 07

Re: Help with Old School Menu

Posted 17 February 2007 - 08:50 PM

Hi-

I looked over everything and most of it I already knew how to do. The scanner class was new.

What I am missing- and again maybe it is just too old school ---

When the program starts I would like a list of options to appear on the screen.

-- I can do that.


Then when a user presses the 1 key on the keyboard I would like to call a method.

--I can call the method I just need to know how to assign the number 1 on the keyboard so it works the same as clicking the mouse on a Button.

--I am going to try this with the scanner class by storing the info in a variable but I thought each key had a number that could be accessed and then I am assuming there is an api available to help work with it.


Ideas?


I am having my students do the tutorials today as they are well written and should reinforce alot of needed skills.

Thanks.

Codeninja


View Postcodeninja, on 17 Feb, 2007 - 11:31 AM, said:

Wow! So much good feedback. I will let my kids try it out and I as well and then post our results!

Tony D aka codeninja the domination

View Postkeems21, on 16 Feb, 2007 - 07:44 PM, said:

If that's really what you mean, taking input from the console, I've got a very easy solution for you.

Use the Scanner class.

import java.util.Scanner.*;

public class ReadConsole
{
  public static void main(String[] args)
  {
	Scanner input = new Scanner(System.in);
	String read = input.nextLine();
	Int num = input.nextInt();
   }
}



The Scanner class is very easy to use, and it prevents you from needing to understand all of the wrapping that goes on with the IO package.

Just create a new Scanner object like I did with the constructor above. Then, everytime you call input.nextLine() or input.nextInt(), the console will wait for you to input some data, which is immediately stored in the variable preceeding the call.

Here's the api in case you need to look something up:
http://java.sun.com/...il/Scanner.html

Enjoy.

Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Help with Old School Menu

Posted 17 February 2007 - 09:02 PM

I'm not sure I understand exactly what you'd like...simply to display your list of choices, then take input from the user, and perform a set of actions based on that input. Is that correct?

import java.util.Scanner.*;

public class ReadConsole
{
  public static void main(String[] args)
  {
	Scanner input = new Scanner(System.in);
	System.out.println("Menu System - Please enter your choice");
	System.out.println("Option 1");
	System.out.println("Option 2");
	System.out.println("Option 3");
	Int num = input.nextInt();
	if(num==1)
	   functionforoption1();
	else if(num==2)
	   functionforoption2();
	else if(num==3)
	   functionforoption3();
	else
	   //default case
   }
}


Of course, there are a million ways to do it - case statements might be more beneficial here.
Was This Post Helpful? 0
  • +
  • -

#10 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 251
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Help with Old School Menu

Posted 17 February 2007 - 11:16 PM

It sounds like what you're asking is if you can set up a KeyListener and catch events for console inputs, and I think (think) the answer is no. It would be kind of cool, but I think you'll have to settle for having to hit enter, validate the input, and then run your method based on the input. If you were allowed to use Swing, you'd be all set.

This post has been edited by alcdotcom: 17 February 2007 - 11:19 PM

Was This Post Helpful? 0
  • +
  • -

#11 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: Help with Old School Menu

Posted 17 February 2007 - 11:28 PM

View Postcodeninja, on 17 Feb, 2007 - 08:50 PM, said:

Hi-

I looked over everything and most of it I already knew how to do. The scanner class was new.

What I am missing- and again maybe it is just too old school ---

When the program starts I would like a list of options to appear on the screen.

-- I can do that.


Then when a user presses the 1 key on the keyboard I would like to call a method.

--I can call the method I just need to know how to assign the number 1 on the keyboard so it works the same as clicking the mouse on a Button.

--I am going to try this with the scanner class by storing the info in a variable but I thought each key had a number that could be accessed and then I am assuming there is an api available to help work with it.


Ideas?


I am having my students do the tutorials today as they are well written and should reinforce alot of needed skills.

Thanks.

Codeninja


OK, now I see what you want. The only problem is that I don't know of any immediate solution. I can't say that I've ever tried to do something like this.

Here's my only thought as of now, create your own UI to replace the terminal/console, then add a key listener (or a key bind) to it.
I'll try and throw something together for you as an example.


And here's that api you were talking about with all of the key variable assignments.
http://java.sun.com/...t/KeyEvent.html
Was This Post Helpful? 0
  • +
  • -

#12 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 251
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Help with Old School Menu

Posted 18 February 2007 - 06:56 PM

View Postkeems21, on 18 Feb, 2007 - 12:28 AM, said:

OK, now I see what you want. The only problem is that I don't know of any immediate solution. I can't say that I've ever tried to do something like this.

Here's my only thought as of now, create your own UI to replace the terminal/console, then add a key listener (or a key bind) to it.
I'll try and throw something together for you as an example.


And here's that api you were talking about with all of the key variable assignments.
http://java.sun.com/...t/KeyEvent.html

The Swing console emulation occurred to me, but he said that they get no credit for using Swing. Plus, it kinda seemed like overkill for their purposes. But it would be interesting to see your implementation. That's the cool thing about programming: everyone will have a different way of doing something.

I still feel like there should be some way to set up a KeyListener and get KeyEvents in a console because I've seen C apps that can do it. But I just don't know how at the moment. I'll do one more quick Google and see if I find anything.
Was This Post Helpful? 0
  • +
  • -

#13 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 251
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Help with Old School Menu

Posted 18 February 2007 - 07:32 PM

Yeah - from all I'm reading, event-based console input has to be done using native libraries (in Windows anyway). However, if you're interested, someone has implemented a Java version of Curses: http://sourceforge.n...ackage_id=30183. You want to talk about old-school, Curses is old-school. :)

Another thought I had was that you could might be able to "cheat" and open an invisible Jframe to catch KeyEvents. I'm not sure if a JFrame can get events when it's invisible because I don't think it can have focus, but it's worth a shot maybe.
Was This Post Helpful? 0
  • +
  • -

#14 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: Help with Old School Menu

Posted 18 February 2007 - 07:47 PM

View Postalcdotcom, on 18 Feb, 2007 - 07:32 PM, said:

Another thought I had was that you could might be able to "cheat" and open an invisible Jframe to catch KeyEvents. I'm not sure if a JFrame can get events when it's invisible because I don't think it can have focus, but it's worth a shot maybe.


I was thinking that exact same thing. Create a JFrame, but never do the JFrame.setVisible(true). I don't think that will work though, because a component has to have focus in order to catch events as far as I know.


Anyway, here's what I've been spending way too much time on.
It's not pretty, but it gets the job done. If you take the time to add a scroll bar and maybe a few other functions, it should work nicely.


First file:
import java.io.PrintStream;
import java.awt.event.*;
import javax.swing.*;

public class Console extends PrintStream implements KeyListener
{
		private JTextArea text;

		public Console(JTextArea text)
		{
				super(System.out);
				this.text = text;
				text.addKeyListener(this);

	}

		public void println(Object o)
		{
				text.append(o.toString() + "\n");

		}

		public void println(String s)
		{
				println((Object) s);
		}

		public void println()
		{
			println((Object) "println\n");
		}

		public void print(Object o)
   	{
			  text.append(o.toString());
				}

			   public void print(String s)
			   {
							   print((Object) s);
			   }

				public void keyPressed(KeyEvent e){}
	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e)
	{
		System.out.println("\nKeyTyped: " + e.getKeyChar() + "\tKeyCode: " + e.getKeyCode());
	}
}



and the main file
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ConsoleMain
{
	public static void main(String[] args) throws Exception //just for fun
	{
		JFrame frame = new JFrame("Console");
		JTextArea text = new JTextArea();

		frame.add(text);


		//frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		frame.setSize(300,500);
		frame.setVisible(true);

		Console myConsole = new Console(text);

		System.setOut(myConsole);
		System.setErr(myConsole);

		System.out.println("Printing in the console");

		try
		{
			throw new Exception("Throwing an exception in the console");
		}
		catch(Exception e)
		{
			System.out.println("\nStackTrace in the console");
			e.printStackTrace();
		}
	}
}



I must say that this has been quite a bit of fun, but I really do have other things I need to take care of.

Feel free to use/abuse/play around with this code to your heart's content.

Enjoy.

Edit:
Sorry about the tabbing, I was jumping from one editor to another for a while.

This post has been edited by keems21: 18 February 2007 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#15 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: Help with Old School Menu

Posted 18 February 2007 - 10:06 PM

I'm pretty sure that I just proved myself wrong:


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class InvisJFrame extends JFrame implements ActionListener
{

	public InvisJFrame()
	{
		JButton button = new JButton("invis");
		button.addActionListener(this);

		this.add(button);

		//this.setVisible(true);
	}

	public void actionPerformed(ActionEvent evt)
	{
		System.out.println(evt.getActionCommand());
	}

	public static void main(String[] args)
	{
		InvisJFrame frame = new InvisJFrame();
		System.out.println(frame.isFocusableWindow());
	}
}



Whether or not the frame is set to visible, the method isFocusableWindow() returns true. So in theory, you should be able to interact with a window even if you can't see it. I tried getting a button to click while the frame was invisible, but it didn't work.

Again, I'm still not sure that this would work because giving an invisible frame focus would take the focus away from the terminal/console that you are supposed to be typing into.

All while I'm doing this, I keep thinking of keyloggers. This is exactly what they do, they record button clicks without any visible UI. I wonder if it's possilbe to make a keylogger in Java. Anyway, that a mystery for another day.

Good luck sifting through all of this stuff codeninja. We're here for you if you need anything else.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2