System.out in GUI, or something similar

Is it posible to show some information from anywhere to some component like JTextArea.System.out.print(String) is posible to use everywhere, it is posible to do tha same just print text not to console but to some component?I hope yuo understant what I'm asking about.
[289 byte] By [Lauris_Ka] at [2007-11-26 13:19:11]
# 1
As long as you have a reference to the text area you want to print to, you should be able to. System.out is ubiquitous because it's static, so you can call it without a reference. Writing to a text area is not static, so you have to have a reference to the component.
hunter9000a at 2007-7-7 17:45:31 > top of Java-index,Java Essentials,New To Java...
# 2
I have something like this at home but can't get to it now.Basically you want to make a TextAreaOutputStream that outputs everything to a given textarea (you can find examples of this on the internet) and then use System.setOut(...) with your stream
tjacobs01a at 2007-7-7 17:45:31 > top of Java-index,Java Essentials,New To Java...
# 3

Wait a second; I've got something for that. <dig dig shuffle shuffle> Ah!

Here it is:public class TextAreaOutputStream extends OutputStream {

private static final int TABSTOP= 4;

private static final int SIZE= 0x8000;

private static final boolean WORDWRAP= true;

private static final int ROWS= 24;

private static final int COLS= 80;

protected JTextArea ta;

public TextAreaOutputStream() { this(new JTextArea(ROWS, COLS)); }

public TextAreaOutputStream(JTextArea ta) { this(ta, TABSTOP, WORDWRAP); }

public TextAreaOutputStream(JTextArea ta, int tabstop, boolean wordwrap) {

this.ta= ta;

initialize(tabstop, wordwrap, false);

}

private void initialize(int tabstop, boolean wordwrap, boolean editable) {

ta.setTabSize(tabstop);

ta.setWrapStyleWord(wordwrap);

ta.setEditable(editable);

}

protected void append(String text) {

int lt= text.length();

synchronized(ta) {

if (lt >= SIZE) {

ta.setText(text);

return;

}

String buf= ta.getText();

int i= 0;

lt+= buf.length();

for (int j= 0; lt-i >= SIZE; i= j+1)

if ((j= buf.indexOf('\n', i)) < 0)

break;

if (lt-i >= SIZE)

ta.setText(buf= text);

else

ta.setText(buf= buf.substring(i)+text);

ta.setCaretPosition(buf.length());

}

}

public JTextArea getJTextArea() { return ta; }

public void write(int b) { append(""+((char)b)); }

public void write(byte b[]) { write(b, 0, b.length); }

public void write(byte b[], int ofs, int len) { append(new String(b, ofs, len)); }

}

Simply hook this up to a System.out or Sytem.err stream and you're in business.

kind regards,

Jos

JosAHa at 2007-7-7 17:45:31 > top of Java-index,Java Essentials,New To Java...
# 4

> I have something like this at home but can't get to

> it now.

>

> Basically you want to make a TextAreaOutputStream

> that outputs everything to a given textarea (you can

> find examples of this on the internet) and then use

> System.setOut(...) with your stream

So it is posible to show something lite target with System.setOut(..) and just use System.out to print in that component?

Lauris_Ka at 2007-7-7 17:45:31 > top of Java-index,Java Essentials,New To Java...
# 5
yup see josah's post
tjacobs01a at 2007-7-7 17:45:31 > top of Java-index,Java Essentials,New To Java...