I am developing a cmd like console using jTextArea, for an IDE. What I want to do is as I click the execute button, it provides me interactive input/output screen in the jTextArea.
I am able to grab the output contents of the cmd screen, but what if user wants to run a command like take two integers as input from stdin (here from my jTextArea) and display the sum, on my console screen, as if it happened in a normal cmd/terminal.
How do I send input to that instance/session?
See java.lang.Process, method java.lang.Process.getOutputStream()
The name OutputStream is a doubtful choice because it's the input of the underlying process, the output for your program.
Related
How can I do password masking (to hide an input text from terminal window) in Java?
I got the solution of using the console but I want to do it in an IDE (BLUEJ) ,which is not possible using the console. I want help.
The console in any IDE or terminal/command line can not be edited after something is printed out in it and simply prints what you ask of it and takes in input that you type.
However, if you are working with a GUI in Swing or JavaFX, you can display your text in a JPasswordField or PasswordField which hides the input as its typed while still storing the correct string values.
I'm making a tic-tac-toe game in Java and so far it's going well, but as of now, I've written my program to print to console. Is there a command to get what would currently be displayed on my console output to show up on a separate window? Something like a JOptionPane?
My console right now looks like a ticTacToe grid, and I want to maybe get a while loop to display the currentBoard to a pop-up-dialog after every set of turns. So the player can see the updated Board after every turn set.
Is this possible?
You can redirect System.out messages to a JTextArea or JTextPane.
Check out the Message Console class.
I am trying to make a program that outputs some data when I press a physical key on my keyboard. Is there anyway to do this without having either the cmd selected or a gui?
Thanks, Not sure how clear this sounds, but if you need clarification ask and I'll try again
I am a huge newbie and I have a program that normally prints items to the Java console window. I would like this program to become a window in which the user can interact with. The reason why I have not resorted to dialog boxes and panels is because this program require multiple prints to the console window. A traditional dialog box does not continuously update or compound on data that has already been printed on the box. I realize that there is another way of doing this by creating a program that mimics the Java console window. Because I am a noob, all of the java console redirecting questions and answers on this site have blown over my head. Can anyone please help me?
See maybe How to Use Editor Panes and Text Panes will be helpful and give you some ideas.
The short answer is, every time you want to update the contents of a text box, call the setText function again. There's no "append" function on the contents: you have to give the entire contents each time. If you want something that mimics a console window, where messages continue to scroll, the simplest thing to do is to keep the entire contents in a StringBuilder. Each time you get new text append to the StringBuilder, then setText(myStringBuilder.toString).
You could, I supppose, write mybox.setText(mybox.getText()+"new contents"). That would be a little inefficient but probably not a big deal.
I don't know exactly what you're up to, but trying to redirect console output to a text box sounds like more nuisance that it's worth. Just put your data in the text box: don't write it somewhere else, then try to get it back and put it where you want it. I suppose if you have thousands of lines of code writing to the console and now you want it to go a text box, there might be value in not having to change all that code. But the structure of a console app is so different from the structure of a GUI app that changing the output statements would probably be the least of the things you'd have to rework.
I currently have a program that prints lines of text to the screen in various manners such as 'System.out.println()' statements and for loops the print all elements in an array to screen.
I am now adding a GUI to this program. My problem is that I want to print everything that prints to eclipse's console to a textbox in my GUI instead. Is this possible and if so how would I go about doing this.
Thanks in Advance.
Check out this blog article, entitled Redirecting System.out and System.err to JTextPane or JTextArea. It describes almost everything you need.
The basic idea is that you create your own specialized output stream. In your implementation of the write() methods, you call some code to append the new data to your text box. Then, you set this new output stream as your System.out by calling System.setOut() or System.setErr().
NOTE: that article is missing one thing. You need to start your program in a separate thread.
An idea:
Create your own PrintStream that outputs everything to this textbox.
Then set this new PrintStream to be the standard output stream like that:
System.setOut(myPrintStream());