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
-
Did you mean something like Tomcat commands to start and stop?Paul Vargas– Paul Vargas2013-03-26 00:28:47 +00:00Commented 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....PulsePanda– PulsePanda2013-03-26 00:29:31 +00:00Commented Mar 26, 2013 at 0:29
-
1@PaulVargas rather Scanner that detects 'exit' typed in cmd linedantuch– dantuch2013-03-26 00:29:46 +00:00Commented 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...Floris– Floris2013-03-26 00:29:54 +00:00Commented Mar 26, 2013 at 0:29
-
1you could have a a timer running, that checks a simple if statement?Anthony Raimondo– Anthony Raimondo2013-03-26 00:34:20 +00:00Commented Mar 26, 2013 at 0:34
|
Show 2 more comments
3 Answers
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.