Results 1 to 2 of 2
  1. #1
    viking90 is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default Using SwingWorker

    I have a JButton that exchanges the contents of two JTextAreas:
    Java Code:
    String temp = leftTextArea.getText();
    leftTextArea.setText(rightTextArea.getText());
    rightTextArea.setText(temp);
    Let's assume that this operation can be quite time-consuming (i.e. moving large chunks of text). Therefore, I should put this into a separate thread, so that user can use the GUI while the text in both of the JTextAreas is being processed.
    I have read about SwingWorker class, but since I've never used that before, I am not quite sure how to do this using the SwingWorker.

    This is what I've come up with so far:
    Java Code:
    new SwingWorker<Void, Void>() {
    		String text1, text2;
    		public Void doInBackground() {
    	        	  text1 = leftTextArea.getText();
    	        	  text2 = rightTextArea.getText();
    	        	  return null;  
    	        }
    	        public void done() {
    	        	  leftTextArea.setText(text2);
    	        	  rightTextArea.setText(text1);	        	  
    	        }
    }.execute();
    Is the above code OK? Or should I move the two lines in the done() method to the doInBackground() and leave the body of done() empty?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Don't exchange those Strings but exchange the Documents between those two JTextAreas.

    kind regards,

    Jos

Similar Threads

  1. Swingworker or new thread
    By cotarelo in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-24-2010, 07:59 PM
  2. SwingWorker question
    By cotarelo in forum Threads and Synchronization
    Replies: 16
    Last Post: 03-23-2010, 12:29 PM
  3. SwingWorker Problem
    By Berkan in forum Threads and Synchronization
    Replies: 10
    Last Post: 03-11-2010, 04:28 AM
  4. SwingWorker Opinions
    By frejon26 in forum AWT / Swing
    Replies: 3
    Last Post: 04-13-2009, 09:41 PM
  5. swingworker
    By musiigedeo in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2007, 01:59 PM

Tags for this Thread

Posting Permissions

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