3

I'm making a server, and it is on a Text Based Raspberry Pi. basically, everything is running from the command line, so when the server runs, there is no graphics, and it prints everything out using System.out.println();. so my question is, instead of having a button that runs a shutdown() method, how can i make it so at any point in time, i am able to push, say, 'e', and the program will run the shutdown() method? i've done some searching, and am not sure quite how to phrase the question. i was thinking adding a keylistener, but im not sure if that can be added to nothing graphic? anyway, any help would be appreciated!!! thanks in advance

7
  • Did you mean something like Tomcat commands to start and stop? Commented Mar 26, 2013 at 0:28
  • honestly, im not sure what tomcat is, but more like, i made a method called shutdown() that runs everything. all i need is to push a key to shut it down, but i dont know how to.... Commented Mar 26, 2013 at 0:29
  • 1
    @PaulVargas rather Scanner that detects 'exit' typed in cmd line Commented Mar 26, 2013 at 0:29
  • Can you show the smallest program that processes commands as you want, with your attempt to catch the "shutdown" keystroke? It will be easier to add some code to that, than to answer this from scratch... Commented Mar 26, 2013 at 0:29
  • 1
    you could have a a timer running, that checks a simple if statement? Commented Mar 26, 2013 at 0:34

3 Answers 3

1

To register keyboard events you first need to have the focus on your program, and for that you need a gui. I suggest:

1.- Create a JLabel(and a scrollbar).

2.- Instead of using System.out.print("text");, use myJLabel.append("text" + "/n");. 

3.- Add a keyboard listener. Register key events so that the x key closes your server, the s key stops it, ...

As you have described your app, you can't write input to the server, you can only read output from it. I recomend step 4.

4.-Add a JTextFiel to send input to the server.

5.-To make it user-friendly , you could use a JEditorPane instead of a JLabel, and add HTML to your output.
Sign up to request clarification or add additional context in comments.

Comments

0

Alright, so based on the comments, (which i up voted btw), i made a thread that constantly used scanner to see if i typed "exit". Thanks for all the help!

Comments

0

If you don't want to have a visible gui, you could create an "always-focused invisible grafical interface" with the listener.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.