Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.out.println to JTextArea

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 == '\r')
            return;

        if (b == '\n')
        {
            final String text = sb.toString() + "\n";
            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,

like image 928
Joe Avatar asked Feb 05 '13 11:02

Joe


People also ask

What is JTextArea in Java?

A JTextArea is a multi-line area that displays plain text. It is intended to be a lightweight component that provides source compatibility with the java. awt. TextArea class where it can reasonably do so.

How do I save a text file as JTextArea?

Saving JTextAreas to a text file. getText(); FileWriter fw = new FileWriter ("Nick. java"); BufferedWriter out = new BufferedWriter (fw); PrintWriter outFile = new PrintWriter (bw); for(int i=0; i<10; i++) { out. write(text); } out.

Is JTextArea editable?

The JTextArea class provides a component that displays multiple lines of text and optionally allows the user to edit the text.


1 Answers

The following example creates frame with text area and redirects System.out to it:

import java.awt.BorderLayout;
import java.awt.Container;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class JTextAreaOutputStream extends OutputStream
{
    private final JTextArea destination;

    public JTextAreaOutputStream (JTextArea destination)
    {
        if (destination == null)
            throw new IllegalArgumentException ("Destination is null");

        this.destination = destination;
    }

    @Override
    public void write(byte[] buffer, int offset, int length) throws IOException
    {
        final String text = new String (buffer, offset, length);
        SwingUtilities.invokeLater(new Runnable ()
            {
                @Override
                public void run() 
                {
                    destination.append (text);
                }
            });
    }

    @Override
    public void write(int b) throws IOException
    {
        write (new byte [] {(byte)b}, 0, 1);
    }

    public static void main (String[] args) throws Exception
    {
        JTextArea textArea = new JTextArea (25, 80);

        textArea.setEditable (false);

        JFrame frame = new JFrame ("stdout");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        Container contentPane = frame.getContentPane ();
        contentPane.setLayout (new BorderLayout ());
        contentPane.add (
            new JScrollPane (
                textArea, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
            BorderLayout.CENTER);
        frame.pack ();
        frame.setVisible (true);

        JTextAreaOutputStream out = new JTextAreaOutputStream (textArea);
        System.setOut (new PrintStream (out));

        while (true)
        {
            System.out.println ("Current time: " + System.currentTimeMillis ());
            Thread.sleep (1000L);
        }
    }
}
like image 165
Mikhail Vladimirov Avatar answered Oct 06 '22 17:10

Mikhail Vladimirov