[Java] GUI Console Output

Normally, if you write an application, to get the console output, you need to either load it in command propmt or write a batch file to have the Jar file launched with the command prompt window. This method might not work at some locations (ie. Comapny Shared Folder).

To solve this problem, we can create out own Output window in Java, and redirect  System.out to that window by overriding the methods.

[Credits] the original codes are found at this site by Mr. Hung Huynh:
http://unserializableone.blogspot.com/2009/01/redirecting-systemout-and-systemerr-to.html

 

So here’s what we will do for example. First, in Netbeans, create a new desktop application project, and drag a jTextArea in the GUI, Netbeans should call it jTextArea1 by default. 

Now go into the source, and add in the following codes:


//The run method is just a method I create to test the output. You can call it in the constructor methods after initComponents(); , or you can try to do a System.out.println(); in the about box action

 public void run()
    {
        redirectSystemStreams();
        System.out.println("THIS IS A DEMO FOR REDIRECTING OUTPUT TO GUI");

                
        System.out.println("--------------------------------------------");
        
        for(int i = 0; i < 30; i++)
        {
            System.out.print(Math.random()+"\t");
            System.out.println("<<End");
        }
        
        System.out.println("--------------------------------------------");
        
                   
    }
    
//The following codes set where the text get redirected. In this case, jTextArea1    
  private void updateTextArea(final String text) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        jTextArea1.append(text);
      }
    });
  }

//Followings are The Methods that do the Redirect, you can simply Ignore them. 
  private void redirectSystemStreams() {
    OutputStream out = new OutputStream() {
      @Override
      public void write(int b) throws IOException {
        updateTextArea(String.valueOf((char) b));
      }

      @Override
      public void write(byte[] b, int off, int len) throws IOException {
        updateTextArea(new String(b, off, len));
      }

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

    System.setOut(new PrintStream(out, true));
    System.setErr(new PrintStream(out, true));
  }


Advertisements

5 thoughts on “[Java] GUI Console Output

  1. Thank you for this tutorial! Would you know how to edit this code so that it updates the JTextArea real-time, instead of when the program finishes?

  2. can i ask…my desired output’s data is in another class…in my GUI class how i call the class to make sure it is output in my application

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s