I have a console application that uses System.out.println's to output text. What I want to do is turn it into an applet, where instead of System.out.println's, it displays the text in a text box. Is there a relatively easy way to convert this?
Message Console is a simple class that will allow you to redirect ouput to a JTextArea or JTextPane.
Is there a relatively easy way to convert this?
Sure, all you need to do is create an applet1 and add a single JTextArea2 to it. And then instead of your System.out.println(str)'s, you do textArea.setText(str) (or textArea.setText(textArea.getText() + "\n" + str) to append text)
http://download.oracle.com/javase/tutorial/deployment/applet/
http://download.oracle.com/javase/tutorial/uiswing/components/textarea.html
EDIT
And to get user input, checkout the showInputDialog method from the JOptionPane class. Examples can be found here:
http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
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've got a huge method that prints multiple lines, numbers, characters and uses system.out and multiple data types, it works. But I'd like to use it in a jframe. I tried converting every system.out statement to a jtextArea.setText(), and did casting for non string types but nothing comes out when I run it.
Is it possible? what is the right way of doing that.
jtextarea right for my method.
If you wish to append text to a JTextArea, use the append method. Right now your code is using setText, which does just that: sets the text of the JTextArea, removing all previous text in the process (and in this way your code seems to almost guarantee that the JTextArea either contain no text, or contain a single new line character).
try jTextField if its only gonna output.
Try jTextField.append if the user will write stuff on the text.
I am designing a chat system..i am making use of JText area to display chat, and JTextField to enter text.
My question is how to recognize smiley's like ":)" and replace it by corresponding image
on text area? i found no method which will append image on text area..Please help.
http://java-sl.com/tip_autoreplace_smiles.html
You'll have to use a read-only JEditorPane to display HTML instead of a JTextArea in that case.
JTextArea was made to display only multiple rows of text, but correct me if I'm wrong. To display Images you could use the JEditorPane control that will allow you to use html, with simple <img /> tags which will point to a image.
Regarding how to recognize smileys you could create a file / list of common patterns that you would like to support and then simple check if the text contains the pattern with the .contains , or even Regular Expressions.
Update
And with the JEditorPane you will also be to do addition things like say scan for emails or links and automatically convert them so the user can click them, always a nice feature to have.
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());
I want to load up a window using the java swing, something like this
However I want to be able to use it for text input also not just to dump data into it..
any idea how to do it?
JTextAreas are editable by default, so input is trivial. Just put one into a test UI and see for yourself.
From a design perspective, using the same JTextArea for both input and output strikes me as unwise. The only example I can think of is a shell interface, and that imposes stronger limits on how input and output works than a JTextArea will provide out of the box.
I'm not sure if I got the point of your question, but you can get the text the user has typed in using the getText() method:
JTextArea ta = new JTextArea();
String input = ta.getText();
Check out this Sun toturial:
http://java.sun.com/docs/books/tutorial/uiswing/components/textarea.html
In particular, scroll down to the "TextAreaDemo" example.