0

How can I show the last... Let's say 5 lines of my programs console output in a GUI? I also want a text box to enter input rather than buttons. Is this possible?

My program is a texted based decision making game in which users give one of three possible responses to the question to progress in the game down different paths

tehp
  • 5,018
  • 1
  • 24
  • 31
  • Rework your program to use Swing. – Thorbjørn Ravn Andersen Nov 15 '14 at 09:39
  • 1
    Sounds like you might be barking up the wrong tree. You won't need to *redirect* console output to the GUI if you direct the output to GUI instead of/in addition to the console in the first place. Solution by avoidance. :-) – newcoder Nov 15 '14 at 09:54
  • 1
    There's also good info about console redirection at http://stackoverflow.com/questions/4334808/how-could-i-read-java-console-output-into-a-string-buffer – newcoder Nov 15 '14 at 09:56

2 Answers2

1

Well, there are some solutions, like JTextArea as console

But I think you need to separate you IO logic from you business logic. That's for what MVC is used.

So, first of all, move all code that is "game" to some class (e.g. Game) and pass all input to its object. Then, create a form, and pass all input from controls to your Game's object.

pbespechnyi
  • 2,251
  • 1
  • 19
  • 29
1

I don't get the point in forwarding the console's output in the TextArea or GUI.Instead directly show it in the GUI!

Nevertheless you can use the generated output from some manipulations to print it in the TextArea.

Regarding your first question on output:

Say you want to print a string.

In console you would do, System.out.println("Hello World");

In TextField you would do it as textField.setText("Hello World"); //Depending on the library.

So you can use it as a medium of I/O.

Regarding you second question on input:

Surely you can take input from the user via a text box and get it in the code using

String input = textField.getText(); //Depending on the library.

After getting the input you can compare it with your desired 3 inputs and do the appropriate job.

Tip: It would be a good idea to use Radio Buttons or Check Boxes if there are exactly 3 possible inputs depending on the use. User would prefer a quick operation instead of writing a phrase.

rene
  • 41,474
  • 78
  • 114
  • 152
Siddharth Kamaria
  • 2,448
  • 2
  • 17
  • 37