JTextArea as console

nicki picture nicki · Nov 7, 2013 · Viewed 19.9k times · Source

I have posted two pieces of code below. Both codes work fine individually. Now, when I run the file Easy, and click on the "Start" button, I want the class AddNumber to be implemented. I mean to say that, instead of the AddNumber running on the console, is there any way I could make AddNumber run in the JTextArea i have created in the first class upon clicking the "Start" button? I thought maybe by action listener?(the way we do in case of buttons) But I'm not sure. Is there any other way to make my JTextArea act as a console for the other .java files?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class Easy extends JFrame{

    JTextArea text=new JTextArea();
    JPanel panel=new JPanel(new GridLayout(2,2));

    JButton button1 =new JButton("Start");

    public Easy(){

        panel.add(text);

        panel.add(button1);
        add(panel,BorderLayout.CENTER);

        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae){
            //add code to call the other class and make the JTextArea act as a console
            }
        });
   }

   public static void main(String arg[]){
       Easy frame=new Easy();
       frame.setSize(300,100);
       frame.setVisible(true);
   }
}

The second class:

import java.util.Scanner;

class AddNumber
{
    public static void main(String args[])
    {
        int x, y, z;
        System.out.println("Enter two numbers to be added ");
        Scanner in = new Scanner(System.in);
        x = in.nextInt();
        y = in.nextInt();
        z = x + y;
        System.out.println("Sum of entered numbers = "+z);
    }
}

I have seen a few posts talking about PrintStream..but i don't think that applies here. Please help me out. Thanks :)

UPDATE: well i found this link: http://www.codeproject.com/Articles/328417/Java-Console-apps-made-easy#HowtousethisJavaConsole1 and it works in the sense that it shows "Enter two numbers to be added "...but where can the user provide his input?

EDIT: I just had to make a reference of the console in the main method of my class...and it works... well, not exactly as i would've wished to..but partly..the input still has to go from the terminal of the IDE..

Answer

Mr. Polywhirl picture Mr. Polywhirl · Nov 7, 2013

If you do a Google search for: "stdout JTextArea", you will a couple of links to solve your problem.

In the last link, buddybob extends java.io.OutputStream to print standard output to his JTextArea. I included his solution below.

TextAreaOutputStream.java

/*
*
* @(#) TextAreaOutputStream.java
*
*/

import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;

/**
* An output stream that writes its output to a javax.swing.JTextArea
* control.
*
* @author  Ranganath Kini
* @see      javax.swing.JTextArea
*/
public class TextAreaOutputStream extends OutputStream {
    private JTextArea textControl;

    /**
     * Creates a new instance of TextAreaOutputStream which writes
     * to the specified instance of javax.swing.JTextArea control.
     *
     * @param control   A reference to the javax.swing.JTextArea
     *                  control to which the output must be redirected
     *                  to.
     */
    public TextAreaOutputStream( JTextArea control ) {
        textControl = control;
    }

    /**
     * Writes the specified byte as a character to the
     * javax.swing.JTextArea.
     *
     * @param   b   The byte to be written as character to the
     *              JTextArea.
     */
    public void write( int b ) throws IOException {
        // append the data as characters to the JTextArea control
        textControl.append( String.valueOf( ( char )b ) );
    }  
}

The TextAreaOutputStream extends the java.io.OutputStream class and overrides its write(int) method overload, this class uses a reference to a javax.swing.JTextArea control instance and then appends output to it whenever its write( int b ) method is called.

To use the TextAreaOutputStream class, [yo]u should use:

Usage

// Create an instance of javax.swing.JTextArea control
JTextArea txtConsole = new JTextArea();

// Now create a new TextAreaOutputStream to write to our JTextArea control and wrap a
// PrintStream around it to support the println/printf methods.
PrintStream out = new PrintStream( new TextAreaOutputStream( txtConsole ) );

// redirect standard output stream to the TextAreaOutputStream
System.setOut( out );

// redirect standard error stream to the TextAreaOutputStream
System.setErr( out );

// now test the mechanism
System.out.println( "Hello World" );