event

VetoableChangeListener example

With this example you are going to learn about a very important event monitoring component, the VetoableChangeListener. The VetoableChangeListener is , in many ways, similar to the PropertyChangeListener class. The main difference is that PropertyChangeListener is applied to bound properties. On the other hand the VetoableChangeListener is applied to constraint properties.

A bound property is just as simple as a property. A constraint property is a property that can change its state only if the event listener allows it to. For example, if the new value of the property is bigger that the listener can allow, it can refuse to give the new value to the property.

In order to work with a VetoableChangeListener one should take the following steps:

  • Use the addVetoableChangeListener method of an event manager and give the new class as an argument to bundle an event with a VetoableChangeListener.
  • Create a new class that implements the VetoableChangeListener interface. Override the vetoableChange method and examine the old and the new value of the property. If you are not happy with the new value you can throw a PropertyVetoException

Let’s take a look a the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;

public class VetoableChangeListenerExample {

    public static void main(String[] argv) {

  KeyboardFocusManager.getCurrentKeyboardFocusManager().addVetoableChangeListener(

    new FocusVetoableChangeListener());
    }
}
class FocusVetoableChangeListener implements VetoableChangeListener {

    @Override
    public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {

  Component oldComp = (Component) evt.getOldValue();

  Component newComp = (Component) evt.getNewValue();

  if ("focusOwner".equals(evt.getPropertyName())) {

if (oldComp == null) {

    System.out.println(newComp.getName());

} else {

    System.out.println(oldComp.getName());

}

  } else if ("focusedWindow".equals(evt.getPropertyName())) {

if (oldComp == null) {

    System.out.println(newComp.getName());

} else {

    System.out.println(oldComp.getName());

}

  }

  boolean vetoFocusChange = false;

  if (vetoFocusChange) {

throw new PropertyVetoException("message", evt);

  }
    }
}

 
This was an example on how to use VetoableChangeListener .

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button