Results 1 to 6 of 6
  1. #1
    mytremea is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default SwingWorker vs setText

    Hello there!

    So i'm basically trying to change a label with a value which comes from a SwingWorker.

    Here's the code:

    Java Code:
      private class Receiver extends SwingWorker<String, String> {
            
            @Override
            protected String doInBackground() throws Exception {
                
                return client.receiveKey();
                
            }
            
            @Override
            protected void done() {
                try {
                    // Y U NO WORK???
                    //keyDoneLabel.setText(get());
                    
                    // AND U WORK?
                    System.out.println(get());
                } catch (Exception exc) {
                    exc.getMessage();
                }
            } 
    
        }
    The thing is (as commented on method done):
    I'm able to print the correct result on Console using get(), but setting the label text to get() will leave the label blank.

    Any ideas?

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

    Default Re: SwingWorker vs setText

    Is the keyDoneLabel object in the done method the same as the one that is visualized on the screen? Can't say based on the code posted, so you may have to show us more.

  3. #3
    mytremea is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: SwingWorker vs setText

    Yes, this Swing Worker class is an internal class from my JFrame class.

    Plus I'm able to check that the label is the same on screen using the debbuger.

    This is really strange. i'm away from my Mac now, but when possible i'll put more code here, if needed indeed.

    Plus, using

    Java Code:
    JOptionPane.showMessageDialog(null, new JLabel(get()))
    also works, displaying the dialog with the data I need.
    Last edited by mytremea; 11-13-2011 at 03:33 PM.

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

    Default Re: SwingWorker vs setText

    Quote Originally Posted by mytremea View Post
    Yes, this Swing Worker class is an internal class from my JFrame class.
    That doesn't guarantee that we're working with the same instance, and in fact until we see more code, anything goes.

    Plus I'm able to check that the label is the same on screen using the debbuger.
    How does the debugger tell you this?

    This is really strange. i'm away from my Mac now, but when possible i'll put more code here, if needed indeed.
    Yes this is what you'll need to do.

  5. #5
    mytremea is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: SwingWorker vs setText

    Ok! :D

    So I'm putting here the constructor of my class:
    Java Code:
    /**
     *
     * @author Zaquia
     */
    public class Gui_Client extends javax.swing.JDialog {
    
        private static ClientSnob client = null;
        
        /** Creates new form Gui_Server */
        public Gui_Client(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
            
            DocumentFilter textCheck = new TextToEncodeFilter();
            ((AbstractDocument)jTextArea1.getDocument()).setDocumentFilter(textCheck);
            
            if (client == null) {
                client = new ClientSnob();
            }
            
            imageLabel.setIcon(null);
            resultLabel.setText(null);
            ipLabel.setText(client.getIP());
            keyDoneLabel.setText("");
            
            fillKeyLabel();
             
        }
    - I'm using dialogs because I've found this would be good enough. I really don't think this could be the reason.
    - Plus, the instruction keyDoneLabel.setText(""); works, changing the label text from jLabel3 to empty.

    So, method fillKeyLabel() is here:
    Java Code:
        private synchronized void fillKeyLabel() {
            (new Receiver()).execute();
        }
    It will instantiate and execute the Receiver class:
    Java Code:
      private class Receiver extends SwingWorker<String, String> {
            
            @Override
            protected String doInBackground() throws Exception {
                
                return client.receiveKey();
                
            }
            
            @Override
            protected void done() {
                try {
                    // Y U NO WORK???
                    //keyDoneLabel.setText(get());
                    
                    // AND U WORK?
                    System.out.println(get());
                } catch (Exception exc) {
                    exc.getMessage();
                }
            } 
    
        }
    I think that calling the variable client inside this intern class will call the same client from the external class: Am I right?

    If not, is there a good way to pass the same pointers to this internal class?

    Regards!
    Last edited by Fubarable; 11-13-2011 at 04:22 PM. Reason: corrected code tags!

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

    Default Re: SwingWorker vs setText

    Moderation: code tags corrected in your post above. You should use the slash for the bottom tag: [/code], not the backslash: [\code].

    I'm still not sure what's going on. You may need to post an SSCCE.

Similar Threads

  1. setText is not firing...! Why?
    By N00Bie in forum New To Java
    Replies: 14
    Last Post: 02-20-2011, 10:59 PM
  2. setText() problem
    By Jozo in forum Java Applets
    Replies: 4
    Last Post: 04-27-2010, 06:29 AM
  3. setText in JTextArea
    By hero in forum AWT / Swing
    Replies: 1
    Last Post: 10-12-2009, 10:38 PM
  4. setText() problem
    By jls7168 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 11:34 PM
  5. setText in event
    By sniezna.stopa in forum SWT / JFace
    Replies: 0
    Last Post: 06-20-2008, 03:56 PM

Posting Permissions

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