Is there any way to make my own System.out? I want to have a class "MyConsole" with an attribute out (PrintWriter) and send from all classes txt messages that will be printed in a JTextArea (like System.out print all messages in the console) without make a instance of it in all classes or pass it in the attributes
Asked
Active
Viewed 499 times
1
-
Not as a static reference without doing some really bad design. – Sotirios Delimanolis Sep 20 '13 at 16:07
-
2If you really have to, create your own `PrintWriter`, then use `System.setOut(myPrintWriter)`, so that every output to the console will be redirected to your printwriter. – BackSlash Sep 20 '13 at 16:07
-
i want to have multiple "System.out" to link more consoles – Sorin George Budescu Sep 20 '13 at 16:12
-
@SorinGeorgeBudescu Create as many `PrintWriter`s you need, set one as output stream with `System.setOut(myPrintWriter)` and use it to propagate the output to the others. – BackSlash Sep 20 '13 at 16:14
-
i need something like ex: Console1.out.println(message1) and the message 1 goes only in the JTextArea linked Console1, Console2.out.println(message2) and the message 2 goes only in the JTextArea linked Console2 – Sorin George Budescu Sep 20 '13 at 16:20
3 Answers
2
i need something like ex: Console1.out.println(message1) and the message 1 goes only in the JTextArea linked Console1, Console2.out.println(message2) and the message 2 goes only in the JTextArea linked Console2
Why don't you just make your own class that extends JTextArea
and write a method to do what you want? Something like:
public class MyTextArea extends JTextArea {
public void println() {
//add text to the textarea
}
}
If you don't need your method to have name println
you can just use the append
method, which already exists in the JTextArea
:
textArea.append(myString+"\n");

BackSlash
- 21,927
- 22
- 96
- 136
0
Create a static final class and static methods and use it through out the application. Static method can be directly called without instantiating the class.

Abhijith Nagarajan
- 3,865
- 18
- 23
-
can i link the jtextarea from my JFrame in a static final class? – Sorin George Budescu Sep 20 '13 at 16:11