0

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?

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);

        }
    }
}
rebeliagamer
  • 1,257
  • 17
  • 33
Quilty Kim
  • 455
  • 1
  • 4
  • 18

2 Answers2

0

Your infinite loop probably prevents the label from updating. You need something like UI thread, that will update it asynchronously.

The Lama
  • 21
  • 4
  • Thank you for your response. I'm not sure what a thread is as I'm following an online Java course that hasn't covered the topic at the time I've been working on this. A brief look at what UI thread does confused me a bit more and I'm wondering if there might not perhaps be a simpler way to achieve my goal of updating a label as described above. so if I understand correctly, the add(label) and remove(label) should not be encapsulated in the same while loop? I intended on making the loop infinite just so I can test it out for various cases and for debugging,had it not exhibited the bug above – Quilty Kim Feb 06 '14 at 17:24
0

In this case the problem correspong to a faulty logic, as you changed the label variable pointer previous remove command, the program just have nothing to remove from canvas as the object addressed by the label variable has not been added to the canvas.

In this case, you should first remove the label object form canvas previous redirection of the pointer by the new command. You just have to remember that the label is a pointer to an object.

import acm.graphics.*;
import acm.program.*;

    public class testCanvas extends ConsoleProgram {

        public void run()
        {
            GCanvas canvas = new GCanvas();
            add(canvas);
            String entry ="";
            GLabel label = null;
            while(true) {

                entry += readLine("Give me a word: ");
                if(entry=="") break;

                if (label!=null) canvas.remove(label);  //removes the previous label object
                label = new GLabel(entry);  //redirect the pointer to the new object
                label.setLocation(200-label.getWidth()/2, 
                        60+label.getHeight());

                label.setFont("Times New Roman-24");
                canvas.add(label);  //adds the new label to the canvas

            }
        }
    }
Odiseo
  • 21
  • 5
  • Another detail that I observed is that you add each new line to the previous value of the entry variable, check if that's really what you want to do. I would had probably used an assigment << entry = readLine("Give me a word: ") >> instead. – Odiseo Jun 15 '14 at 07:19