Skip to content

Cancelable Events

cyklon73 edited this page Apr 11, 2024 · 1 revision

Cancelable events can be canceled by listeners. To create a cancelable event, extend the Event class and implement the Cancelable interface

public class MyEvent extends Event implements Cancelable {

}

To implement the methods, create a Boolean variable that is set via setCanceled and queried via isCanceled

private boolean canceld = false;

@Override
public boolean isCanceld() {
	return canceld;
}

@Override
public void setCanceled(boolean canceled) {
	this.canceld = canceld;
}

Alternatively, you can simply extend the CancelableEvent class

with CancelableEvent class

public class MyEvent extends CancelableEvent {

	public MyEvent() {
		super();
	}

}

with Cancelable interface

public class MyEvent extends Event implements Cancelable {

	private boolean canceld = false;
	
	public MyEvent() {
		super();
	}

	@Override
	public boolean isCanceld() {
		return canceld;
	}

	@Override
	public void setCanceled(boolean canceled) {
		this.canceld = canceld;
	}
}
Clone this wiki locally