2

A combo box has two values: AND and OR.

I written property change listener for the Combo,as this event fires, if and only of the currently selected value and previous values are different. But I need that this event should be fired even if the values are same?

This is my sample code snippet:

public void setRuleOperation(String ruleOperation) {
    String oldValue = this.ruleOperation;

    if (oldValue != ruleOperation) {
        this.ruleOperation = ruleOperation;
        getPropertyChangeSupport().firePropertyChange(PROPERTY_OPERATION, oldValue, null);
    }
    
    this.ruleOperation = ruleOperation;
} 
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Naveen Kocherla
  • 499
  • 7
  • 17
  • 1
    this make me (some) sence for editable JComboBox only, use ItemListener – mKorbel Mar 06 '14 at 12:44
  • HI mKorbel, I already tried with this listener but its giving the previously selected value not the currently selected value in combo – Naveen Kocherla Mar 06 '14 at 12:48
  • or you would have to create a Swing Action with `firePropertyChange(mySwingAction, oldValue, newValue);` – mKorbel Mar 06 '14 at 12:48
  • `its giving the previously selected value not the currently selected value in combo` == not true at all, read API or official Oracle tutorial How to use ItemListener – mKorbel Mar 06 '14 at 12:49
  • 3
    If you want it to always fire (even on selecting the same value), add an actionListener. The action listener always fires on changing the combobox. – dARKpRINCE Mar 06 '14 at 14:49
  • 1
    Come off it. If you want to fire the even unconditionally why are you firing it conditionally? and specifically, if you want to fire it even on equaltiy, why did you code an inequality test? Just remove it. – user207421 Jul 26 '20 at 03:33

1 Answers1

0

One possibility entails:

  • Create a PropertyChangeEvent instance using the PropertyChangeSupport object.
  • Create a new fire method to emulate firePropertyChange.
  • Iterate over the property change listeners.
  • Invoke propertyChange for each listener using the new event instance.

Et la voilà, the if conditionals that prevent firing when the old value equals the new value have been skirted.

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 * Responsible for notifying its list of managed listeners when property
 * change events have occurred. The change events will only be fired if the
 * new value differs from the old value.
 *
 * @param <P> Type of property that, when changed, will try to issue a change
 *            event to any listeners.
 */
public abstract class PropertyDispatcher<P> {
  private final PropertyChangeSupport mDispatcher =
      new PropertyChangeSupport( this );

  /**
   * Adds a new listener to the internal dispatcher. The dispatcher uses a map,
   * so calling this multiple times for the same listener will not result in
   * the same listener receiving multiple notifications for one event.
   *
   * @param listener The class to notify when property values change, a value
   *                 of {@code null} will have no effect.
   */
  public void addPropertyChangeListener(
      final PropertyChangeListener listener ) {
    mDispatcher.addPropertyChangeListener( listener );
  }

  @SuppressWarnings("unused")
  public void removePropertyChangeListener(
      final PropertyChangeListener listener ) {
    mDispatcher.removePropertyChangeListener( listener );
  }

  /**
   * Called to fire the property change with the two given values differ.
   * Normally events for the same old and new value are swallowed silently,
   * which prevents double-key presses from bubbling up. Tracking double-key
   * presses is used to increment a counter that is displayed on the key when
   * the user continually types the same regular key.
   *
   * @param p Property name that has changed.
   * @param o Old property value.
   * @param n New property value.
   */
  protected void fire( final P p, final String o, final String n ) {
    final var pName = p.toString();
    final var event = new PropertyChangeEvent( mDispatcher, pName, o, n );

    for( final var listener : mDispatcher.getPropertyChangeListeners() ) {
      listener.propertyChange( event );
    }
  }
}

This can be useful when firing multiple events for key presses using PropertyChangeSupport. Typing "Hello" would bubble up as "Helo" because the old key event ("l") matches the second key press event ("l"). Direct notification in this manner allows the double-"l" to bubble up two distinct key press/release events.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315