Im working on a game right now that uses a scanner to interact with the user. The Game was first text based so the Java console view was what i used to play. Now I have to convert the game to a Gui or Jframe so I want to be able to have the same console feature inside the frame while also being able to still use the scanner. Any ideas? Im new at coding so any help or same code will be greatly appreciated! Thank you
-
2My advice: don't. If you're going to be using a GUI, don't hamper it in this way. – Hovercraft Full Of Eels Apr 20 '12 at 02:06
-
2Hi @HovercraftFullOfEels! Can you suggest what can be done instead? I found this question very interesting. – TechSpellBound Apr 20 '12 at 02:26
-
2A console program is by nature a linear program which limits its abilities to a great degree, while a GUI is an event-driven program whose behavior depends not on where on a line you are, but on the current state of the program coupled with the user's input. If you're going to make a GUI, then don't tie it to a Scanner but instead make it graphical and event driven. – Hovercraft Full Of Eels Apr 20 '12 at 02:31
-
true, forcing your GUI to use Scanner would limit yourself greatly - harakiri-style – Dan May 16 '12 at 13:27
2 Answers
You could create a GUI that looks like a console, such as by having a JTextArea
for displaying the console text, and a JTextField
where the user can enter the information they want to send to the console. By adding a KeyListener
to the JTextField
, you can listen for the user pressing 'Enter' and then send the information to the console.
Rather than reading user input from the console (which sits and waits for the user to finish entering the information, then performs an action), you would simply listen for the 'Enter' key and then call a method, passing through your 'JTextField' text.

- 8,666
- 13
- 43
- 57
There is alot of interest in such a component it seems. Here are some other discussions on SO with good pointers for implementing this:
- Looking for an efficient Java Swing based console
- Create a Command console
- Java Swing console component
- Create Java command console in JPanel
And theres even more if hints if you Google the terms "swing console".

- 1
- 1

- 79,279
- 19
- 185
- 195