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

3
  • Rework your program to use Swing. Commented Nov 15, 2014 at 9: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. :-) Commented Nov 15, 2014 at 9:54
  • 1
    There's also good info about console redirection at stackoverflow.com/questions/4334808/… Commented Nov 15, 2014 at 9:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

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.