user2598911
user2598911

Reputation: 379

Displaying console output in jtextarea even when other actions occur

I found this code here: how to display console output in java JTextarea one by one in a loop when button action is triggered and it displays console output in a jtextarea. I have added this class as action in a jmenuitem. So that it appears when I want and when I run other classes it will show the output there. However when I launch it, it works properly, but if I try and launch another class which will show output in console and accept userinput, the jtextarea which is supposed to show console output at the same time, it freezes. How could I make it so that it stays tuned, despite invoking other classes/frames? Thanks in advance

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DynamicWrite implements ActionListener
{
    JFrame frame = new JFrame("TextArea");
    JTextArea tArea = new JTextArea(10,20);
    JButton button = new JButton("Click");
    JScrollPane pane = new JScrollPane(tArea);
    SwingWorker worker;
    String s= "Java is an Object Oriented Programming langauge...Java is static typed language...asbfldfjsdj";//some random String
    public void prepareAndShowGUI()
    {
        Container container = frame.getContentPane();
        container.add(pane);container.add(button,BorderLayout.NORTH);
        tArea.setLineWrap(true);
        tArea.setWrapStyleWord(true) ;
        button.addActionListener(this);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public void actionPerformed(ActionEvent evt)
    {
        if(evt.getSource()==button)
        {
            tArea.setText("");
            if (worker!=null)
            {
                worker.cancel(true);
            }
            worker = new SwingWorker()
            {
                @Override
                protected Integer doInBackground()//Perform the required GUI update here.
                {
                    try
                    {
                        for(int i = 0;i<s.length();i++)
                        {
                            tArea.append(String.valueOf(s.charAt(i)));
                            Thread.sleep(5);
                        }
                    }catch(Exception ex){}
                    return 0;
                }       
            };
            worker.execute();//Schedules this SwingWorker for execution on a worker thread.
        }
    }   
    public static void main(String st[])
    {
        DynamicWrite dyna = new DynamicWrite();
        dyna.prepareAndShowGUI();
    }
}

Upvotes: 0

Views: 5411

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

The example you have (apart from from being a bad example as outlined by Hovercraft Full of Eels) has nothing to do with redirecting console output.

If you want to redirect the standard out to the text area, take a look at How to set output stream to TextArea for an example

If you want to redirect the output of some other process, then you can only do this if you've launched the process yourself, there's no (easy way that I know of) to connect to the standard out/in of another running process (that your program didn't start itself directly)

Check out Printing a Java InputStream from a Process for an example

Upvotes: 1

Related Questions