Label - TextField intermittent

Hi,
A label that changes the visibility at intervals of 1 second, but does not work.

Can anyone tell me how to fix it?

Thank you in advance.


public class TestApplication extends Application {
	Label label ;
	Window mainWindow;
	@Override
	public void init() {
		mainWindow = new Window("Test Label on/off Application");
		label = new Label("Hello Vaadin user");
		mainWindow.addComponent(label);
		setMainWindow(mainWindow);
		
	
	Timer timer = new Timer (1000, new ActionListener ()
	{
	    public void actionPerformed(ActionEvent e)
	    {
	        
	    	
	    	label.setVisible(! label.isVisible());

	        //This display on Console OK.
	        System.out.println("Visible : " + label.isVisible());
	    	
	     }
	}); 
	timer.start();
	}
}

Your solution can’t work, because it changes the visibility only on the server-side.

The usual way to initiate periodic calls to server is polling with ProgressIndicator. However, even though it causes calls to server, it does not have a way to actually cause any changes in the server-side state. You could do such changes in another server-side thread, such as your Timer there, but the thread would not run synchronously with the polling. The only way to run something with every client-initiated poll request is to use a transaction listener. Just notice that it is called also for any other request.

For example, in the following we have a counter (the example has some weird stuff just to get some proper references):

// A counter to increment each call
class Counter {
    Integer value = 0;
    
    public Integer increment() {
        return ++value;
    }
}
final Counter counter = new Counter();
        
// A component to display the counter value
final Label label = new Label("Count: -"); 
layout.addComponent(label);

// Progress indicator that causes server poll every second
ProgressIndicator indicator = new ProgressIndicator();
indicator.setIndeterminate(true);

// On every poll (OR any other server request),
// increment the counter
getApplication().getContext().addTransactionListener(
        new TransactionListener() {
    public void transactionStart(Application application,
                                 Object transactionData) {
        label.setValue("Count: " + counter.increment());
    }
    
    public void transactionEnd(Application application,
                               Object transactionData) {
    }
});
layout.addComponent(indicator);

Check out the
live example
.

Well, I’m not sure what you are exactly trying to do, but polling is a rather heavy operation just to create a visual effect. Why don’t you just use “[tt]
.v-label {text-decoration: blink}
[/tt]” CSS to get the blinking effect?