Home » Desktop Java » awt » event » VetoableChangeListener example

About Byron Kiourtzoglou

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.

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 .

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of