Results 1 to 2 of 2
  1. #1
    movesquare is offline Member
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Default Label refresh function does not work with acm package

    Hello. I'm trying to setup a function that takes input from a user and prints it onto a label and updates it per entry. The updating would occur through removing the old label and then adding a label with the updated value. The text would be center-aligned. While I'm able to get the label to print the current value of "entry", it does so without removing the label with the old value. I was wondering how I would be able to correct this issue? I tried reversing the add(label) and remove(label) to see if there is a syntax error with remove, and I determined from the label not being there at all that remove was called correctly. I have kept the loop infinite for debugging purposes to work with various test cases as they come to me. Any help is much appreciated.

    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    
    public class testCanvas extends ConsoleProgram {
    
        public void run()
        {
            GCanvas canvas = new GCanvas();
            add(canvas);
            String entry ="";
            while(true)
            {
    
                entry += readLine("Give me a word: ");
                if(entry=="") break;
                GLabel label = new GLabel(entry);
                label.setLocation(200-label.getWidth()/2, 
                        60+label.getHeight());
    
                label.setFont("Times New Roman-24");
                // remove old label and immediately update it with 
                // label with current value for "entry"
                canvas.remove(label);
                canvas.add(label);
    
            }
        }
    }

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Label refresh function does not work with acm package

    Instead of removing and adding the label back, can you just set the text of the label to a different String?

    In Swing, you could do the following:

    label.setText(newValue);

    I imagine that you could do a similar thing in ACM.

Similar Threads

  1. Replies: 1
    Last Post: 06-21-2013, 07:21 PM
  2. Replies: 11
    Last Post: 04-26-2012, 11:46 PM
  3. Replies: 0
    Last Post: 01-03-2011, 04:24 PM
  4. Why the web browser needs to refresh first so that creating table may work? Pls help.
    By MarkSquall in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-29-2010, 12:14 PM
  5. How to make print function for single label?
    By makpandian in forum AWT / Swing
    Replies: 2
    Last Post: 02-12-2009, 06:26 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
  •