how to display console output in java JTextarea one by one in a loop when button action is triggered

后端 未结 3 713
孤街浪徒
孤街浪徒 2020-12-20 09:28

I got a problem with how to display console output in Jtextarea one be one. I have successfully redirected system console output into JTextarea. But the problem is that in r

相关标签:
3条回答
  • 2020-12-20 09:35

    Don't call Thread.sleep in the EDT. This causes the UI to "freeze" and delays expected visual updates. Swing has concurrency mechanisms such as SwingWorker that allow non-visual processing to be done in parallel with the Swing application.

    0 讨论(0)
  • 2020-12-20 09:41

    To make Swing GUI to update dynamically on fire of some event or with some continuous background changes , you can use SwingWorker API provided by Swing. Try this 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<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();
        }
    }
    

    I hope this solves your problem.
    To know more about SwingWorker API watch here

    0 讨论(0)
  • 2020-12-20 09:49

    Please use something like this.

    String s = ("Your String...");
    textArea.append(s);
    
    0 讨论(0)
提交回复
热议问题