16

I'm trying to get the content of console in a textArea in java.

For example if we have this code,

class FirstApp {
    public static void main (String[] args){
        System.out.println("Hello World");
    }
}

and I want to output the "Hello World" to a textArea, what actionPerformed do I have to choose?

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
user618111
  • 439
  • 5
  • 15
  • 27

5 Answers5

10

I found this simple solution:

First, you have to create a class to replace the standard output:

public class CustomOutputStream extends OutputStream {
    private JTextArea textArea;

    public CustomOutputStream(JTextArea textArea) {
        this.textArea = textArea;
    }

    @Override
    public void write(int b) throws IOException {
        // redirects data to the text area
        textArea.append(String.valueOf((char)b));
        // scrolls the text area to the end of data
        textArea.setCaretPosition(textArea.getDocument().getLength());
        // keeps the textArea up to date
        textArea.update(textArea.getGraphics());
    }
}

Then you replace standards as follows:

JTextArea textArea = new JTextArea(50, 10);
PrintStream printStream = new PrintStream(new CustomOutputStream(textArea));
System.setOut(printStream);
System.setErr(printStream);

The problem is that all the outputs will be showed only in the text area.

Source with a sample: http://www.codejava.net/java-se/swing/redirect-standard-output-streams-to-jtextarea

Sertage
  • 3,123
  • 1
  • 19
  • 26
  • 2
    This is almost perfect, but whenever the console outputs the textarea flashed a bunch. Instead of appending, use `textArea.setText(textArea.getText() + String.valueOf((char)paramInt));` instead, it doesn't flash and flows like the original `System.out`. – Cardinal System Apr 26 '17 at 01:22
  • You may have to use SwingUtilities.invokeLater() for this to work. – Claude Martin Aug 24 '17 at 12:16
8

Message Console shows one solution for this.

camickr
  • 321,443
  • 19
  • 166
  • 288
3

You'll have to redirect System.out to a custom, observable subclass of PrintStream, so that each char or line added to that stream can update the content of the textArea (I guess, this is an AWT or Swing component)

The PrintStream instance could be created with a ByteArrayOutputStream, which would collect the output of the redirected System.out

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
3

One of the way you can do it by setting the System OutputStream to a PipedOutputStream and connect that to a PipedInputStream that you read from to add text to your component, E.g

PipedOutputStream pOut = new PipedOutputStream();   
System.setOut(new PrintStream(pOut));   
PipedInputStream pIn = new PipedInputStream(pOut);  
BufferedReader reader = new BufferedReader(new InputStreamReader(pIn));

Have u looked at the following link ? If not, then you must.

Community
  • 1
  • 1
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
1

like this http://img122.imageshack.us/img122/5692/dibujoof2.png

Create Java console inside a GUI panel

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569