Disabled button

Hello, I have some problem with understanding when the method commits the changes. I thought that code bellow must disable button immediately, but it doesn’t.

public class VwidgettestingApplication extends Application {

	void method() {
		for (int i = 0; i < 500000; i++) {
			System.out.println("System.out.println");
		}
	}
	@Override
	public void init() {
		Window mainWindow = new Window("Application");
		Button button = new Button("enabled");
		button.setWriteThrough(true);
		button.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
                                event.getButton().setCaption("disabled");
				event.getButton().setEnabled(false);
				method();
			}
		});
		mainWindow.addComponent(button);
		setMainWindow(mainWindow);
	}
}

What should I do that button will be disabled first.
thx.

Hello,

Did you try to declare your Button as final ?
Then you could access it within your listener, and disable it right away.

Sorry, this code doesn’t work as well.

@Override
	public void init() {
		Window mainWindow = new Window("Application");
		final Button button = new Button("enabled");
		button.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				button.setCaption("disabled");
				button.setEnabled(false);
				method();
			}
		});
		mainWindow.addComponent(button);
		setMainWindow(mainWindow);
	}

I really don’t know how can I work this problem around.

I think you should try to make your application
extends

Button.ClickListener
and then
@Override
the
buttonClick
method within it.

I assume what you mean is that the button only gets disabled after the execution of method().

This is because the whole request from the browser is processed on the server before sending a reply to the browser. The button gets disabled as soon as the server replies to the browser, just after the execution of the listener if there are no other updates from the client still to be processed in the same request.

If you want something to happen on the UI before a long-running task, you should make that task a background thread and use polling or a push mechanism to inform the UI of its completion - see
this post
.

I see, if I want to disable button first, I should use ICEPush plug-in.
Thank you very much.

Strange thing with ICEPush, I wrote simple application:


public class VwidgettestingApplication extends Application {
	private ICEPush pusher = new ICEPush();
	final Button button = new Button("enabled");

	void method() {
		for (int i = 0; i < 500000; i++) {
			System.out.println("System.out.println");
		}
	}

	public class BackgroundThread extends Thread {
		@Override
		public void run() {
			synchronized (VwidgettestingApplication.this) {
				method();
				button.setCaption("enabled");
				button.setEnabled(true);
			}
			pusher.push();
		}
	}

	@Override
	public void init() {
		Window mainWindow = new Window("Application");
		mainWindow.addComponent(pusher);

		button.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				button.setCaption("disabled");
				button.setEnabled(false);
				new BackgroundThread().start();
			}
		});
		mainWindow.addComponent(button);
		setMainWindow(mainWindow);
	}
}

And get an Exception. But not always, sometimes It works well…
Strange, strange, strange.

Exception in thread "Thread-19" java.lang.NullPointerException
	at org.vaadin.artur.icepush.ICEPush.getPushContext(ICEPush.java:73)
	at org.vaadin.artur.icepush.ICEPush.push(ICEPush.java:60)
	at com.example.vwidgettesting.VwidgettestingApplication$BackgroundThread.run(VwidgettestingApplication.java:33)

Not sure if it is related to the problem, but you should probably only synchronize the parts related to the UI to the application instance.
The method() call should not need to be synchronized unless it accesses UI components directly, but maybe the call to pusher.push() should be. If you synchronize the method() call, you limit the responsiveness of the UI (i.e. it can seem to block completely if the user tries to do something) while doing the long background processing.