Results 1 to 3 of 3
  1. #1
    shivam0101 is offline Member
    Join Date
    Mar 2010
    Posts
    28
    Rep Power
    0

    Default Implementing swingworker

    Thought of making a small textEditor. It will contain two textareas, one for entering the text and one for error display. As for my understanding, SwingWorker works in background so there will be no delay in the UI updation. For checking, i wrote the below code and inserted 5000 lines and tried to type, i think as the line goes on increasing, the updation in textEditor2 is becoming very slow. Is the implementation of the swingworker is correct in the code?

    textEditor1 and textEditor2 are JTextarea

    This i have allready posted in stackoverflow.com

    If some how i could manage avoid of making a new worker everytime i press a key i think it should work.

    Thanks in advance
    Java Code:
    private void editorKeyPressed(java.awt.event.KeyEvent evt) {
    
    SwingWorker worker = new SwingWorker<Void, Void>() {
        String text = null;
    
        @Override
        protected Void doInBackground() throws Exception {
            text = textEditor1.getText().toString();
            return null;
        }
    
        @Override
        protected void done() {
            try {
                get();
                textEditor2.setText(text);
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    };
    
    worker.execute();   
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Implementing swingworker

    inserted 5000 lines and tried to type, i think as the line goes on increasing, the updation in textEditor2 is becoming very slow.
    Every update to that huge String copies the whole thing and adds on the new String to create a new Huge String.
    Can you do it another way?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Implementing swingworker

    Cross posted on stack overflow without notifying us. Please understand that it is fine to cross post question, but it is not polite to not notify us that you're doing so as it implies that you don't understand that we're all volunteers and you don't value our time. Please let us know when you do this so that we don't duplicate work that's already been done.

Similar Threads

  1. When do I use SwingWorker?
    By TacoManStan in forum AWT / Swing
    Replies: 8
    Last Post: 10-24-2011, 01:40 AM
  2. SwingWorker
    By 3.14.TR in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-14-2011, 05:53 PM
  3. How to stop SwingWorker?
    By JStarter in forum AWT / Swing
    Replies: 14
    Last Post: 07-20-2010, 05:36 PM
  4. Using SwingWorker
    By viking90 in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 10:17 AM
  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
  •