Results 1 to 2 of 2
  1. #1
    pulkownik is offline Member
    Join Date
    Mar 2014
    Rep Power

    Default adding elements to JText area using loop - frozen screen - should use multithreading?


    I have something like this:

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    public class testing extends JFrame implements ActionListener
    	private JButton start;
    	private JTextArea Area;
    	public testing()
    		setSize(900, 700);
    		start = new JButton("start");
    		start.setBounds(20, 20, 90, 40);
    		Area = new JTextArea();
    		JScrollPane scrollPane = new JScrollPane(Area);
    		scrollPane.setBounds(280, 15, 385, 600);
    	public static void main(String[] args)
    		testing olek = new testing();
    	public void actionPerformed(ActionEvent e)
    		if (e.getSource() == start)
    			for (int i = 0; i < 1000000; i++)
    				Area.append("Number: " + i + " \n");
    When i am adding a lot of staff to text area, application is "frozen". Is any way to adding elements live? so when i press the start button i will see how the text arena is updating with new numbers?

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Rep Power

    Default Re: adding elements to JText area using loop - frozen screen - should use multithread

    There isn't an easy way to do this. Swing is pretty much single-threaded. Most code that interacts with the Swing framework, e.g., everything in your actionPerformed() method, executes on the EDT (event dispatch thread). Since the actionPerformed() method has a tight loop of a million iterations, the processing in the loop pretty much takes over the EDT. This leaves very little capacity for GUI updates, e.g., printing the string onto the JTextArea UI, that also need to be done on this thread, thus causes the GUI to "freeze".

    Perhaps the only thing you can do, apart from using a different GUI toolkit that can handle this kind of load, is to workaround the problem by appending the strings into JTextArea in batches. E.g.,
    • use a separate thread to perform the iterations (offloading the work to a separate thread will free up Swing's EDT for GUI updates)
    • on the separate thread, append the string in each iteration to a StringBuilder object, thereby building a much longer string
    • every, say, 1000 iterations, use SwingUtilities.invokeAndWait() to call the JTextArea.append() method to append the built-up string in the StringBuilder object to the JTextArea.

    Since the iterations are occurring on a separate thread, you need to use SwingUtilities.invokeAndWait() so that JTextArea.append() can take place on the EDT.

    See Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing) to better understand this.

Similar Threads

  1. adding elements to a list
    By mia69 in forum Advanced Java
    Replies: 2
    Last Post: 02-06-2014, 04:26 PM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  4. Adding elements to an ArrayList
    By ArcherSam in forum Advanced Java
    Replies: 7
    Last Post: 01-28-2011, 03:05 PM
  5. JText area help?
    By Jcbconway in forum AWT / Swing
    Replies: 1
    Last Post: 09-16-2010, 01:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts