Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying console output in jtextarea even when other actions occur
    primarykey
    data
    text
    <p>I found this code here: <a href="https://stackoverflow.com/questions/14357418/how-to-display-console-output-in-java-jtextarea-one-by-one-in-a-loop-when-button">how to display console output in java JTextarea one by one in a loop when button action is triggered</a> 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</p> <pre><code>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&lt;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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload