tehp
tehp

Reputation: 6394

Show Java Console in GUI

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

Upvotes: 0

Views: 3729

Answers (2)

pbespechnyi
pbespechnyi

Reputation: 2291

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.

Upvotes: 1

Siddharth Kamaria
Siddharth Kamaria

Reputation: 2717

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.

Upvotes: 1

Related Questions