如果Swing Jcombo框中的新旧值相等,如何触发属性更改侦听器 [英] How to fire property change listener if old and new values are equal in swing Jcombo box

查看:73
本文介绍了如果Swing Jcombo框中的新旧值相等,如何触发属性更改侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

组合框具有两个值:ANDOR.

A combo box has two values: AND and OR.

我编写了Combo的属性更改侦听器,当且仅当当前选定值和先前值不同时,此事件才会触发.但是我需要即使值相同也要触发此事件?

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;
} 

推荐答案

一种可能性涉及:

  • 使用PropertyChangeSupport对象创建PropertyChangeEvent实例.
  • 创建一个新的fire方法来模拟firePropertyChange.
  • 遍历属性更改侦听器.
  • 使用新的事件实例为每个侦听器调用propertyChange.
  • 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 lavoilà,防止在旧值等于新值时触发的if条件.

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 );
    }
  }
}

当使用PropertyChangeSupport触发按键的多个事件时,这很有用.键入"Hello".会冒泡为"Helo"因为旧键事件("l")与第二键按下事件("l")匹配.以这种方式的直接通知允许双"1"字样.使两个不同的按键按下/释放事件发生气泡.

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.

这篇关于如果Swing Jcombo框中的新旧值相等,如何触发属性更改侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆