EDIT: I have edited the post to clarify my question, now I myself, have more understanding.
I am essentially, as the title says, attempting to output console to my JTextArea
in my GUI, whilst performing the tasks of the application.
Here is what I am currently doing:
public class TextAreaOutputStream extends OutputStream
{
private final JTextArea textArea;
private final StringBuilder sb = new StringBuilder();
public TextAreaOutputStream(final JTextArea textArea)
{
this.textArea = textArea;
}
@Override
public void flush()
{
}
@Override
public void close()
{
}
@Override
public void write(int b) throws IOException
{
if (b == '
')
return;
if (b == '
')
{
final String text = sb.toString() + "
";
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
textArea.append(text);
}
});
sb.setLength(0);
}
sb.append((char) b);
}
}
The above will successfully re-direct System.out
to my output stream above and therefore despatch an event to the EventQueue
to update my GUI (JTextArea
).
Here is the issue:
Currently using invokeLater()
will as it says on the docs:
Causes runnable to have its run method called in the dispatch thread of the EventQueue. This will happen after all pending events are processed.
So what I actually want to do is perform my update to the GUI (call run()
) before processing everything else in the EventQueue.
Is it possible to inject an event essentially into my EventQueue? Or can somebody point me to an decent tutorial on this area?
thanks,
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…