我有一些更新JSlider
的代码;在另一个时间点,JSlider
的最大值需要更新。问题是当我在滑块上调用setMaximum()
时,它还会调度ChangeEvent
。为了避免我这样做:
slider.removeChangeListener(this);
slider.setMaximum(newMax);
slider.addChangeListener(this);
是否有更干净/更优雅的方式?
答案 0 :(得分:3)
一种干净的方式可能(取决于你真正需要的东西)来实现一个自定义的BoundedRangeModel,它可以触发一个可以携带实际更改的属性的自定义ChangeEvent:
/**
* Extended model that passes the list of actually changed properties
* in an extended changeEvent.
*/
public static class MyBoundedRangeModel extends DefaultBoundedRangeModel {
public MyBoundedRangeModel() {
}
public MyBoundedRangeModel(int value, int extent, int min, int max) {
super(value, extent, min, max);
}
@Override
public void setRangeProperties(int newValue, int newExtent, int newMin,
int newMax, boolean adjusting) {
int oldMax = getMaximum();
int oldMin = getMinimum();
int oldValue = getValue();
int oldExtent = getExtent();
boolean oldAdjusting = getValueIsAdjusting();
// todo: enforce constraints of new values for all
List<String> changedProperties = new ArrayList<>();
if (oldMax != newMax) {
changedProperties.add("maximum");
}
if (oldValue != newValue) {
changedProperties.add("value");
}
// todo: check and add other properties
changeEvent = changedProperties.size() > 0 ?
new MyChangeEvent(this, changedProperties) : null;
super.setRangeProperties(newValue, newExtent, newMin, newMax, adjusting);
}
}
/**
* Extended ChangeEvent that provides a list of actually
* changed properties.
*/
public static class MyChangeEvent extends ChangeEvent {
private List<String> changedProperties;
/**
* @param source
*/
public MyChangeEvent(Object source, List<String> changedProperties) {
super(source);
this.changedProperties = changedProperties;
}
public List<String> getChangedProperties() {
return changedProperties;
}
}
它的用法类似于:
final JSlider slider = new JSlider();
slider.setModel(new MyBoundedRangeModel(0, 0, -100, 100));
ChangeListener l = new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (e instanceof MyChangeEvent) {
MyChangeEvent me = (MyChangeEvent) e;
if (me.getChangedProperties().contains("value")) {
System.out.println("new value: " +
((BoundedRangeModel) e.getSource()).getValue());
}
if (me.getChangedProperties().contains("maximum")) {
System.out.println("new max: " +
((BoundedRangeModel) e.getSource()).getMaximum());
}
} else {
// do something else or nothing
}
}
};
slider.getModel().addChangeListener(l);
请注意,您必须使用模型注册侦听器,而不是使用滑块(原因是滑块创建普通类型的新changeEvent)
答案 1 :(得分:0)
您可以检查是谁在听众中触发了更改。它仍然非常脏,但您不必删除更改侦听器。
答案 2 :(得分:0)
它看起来像问题一样,实质上是this question.在这种情况下,我担心答案是“不”
答案 3 :(得分:0)
如果您只需要滑块在值更改时触发事件,您可以通过以下方式简化kleopatra的答案:
// make sure slider only fires changEvents when value changes
slider.setModel(new DefaultBoundedRangeModel() {
final ChangeEvent theOne=new ChangeEvent(this);
@Override
public void setRangeProperties(int newValue, int newExtent, int newMin,int newMax, boolean adjusting)
changeEvent= (getValue() != newValue ? theOne:null);
super.setRangeProperties(newValue, newExtent, newMin, newMax, adjusting);
}
@Override
protected void fireStateChanged()
{
if(changeEvent==null) return;
super.fireStateChanged();
}
});
然后你需要做的就是在模型上注册一个“标准”ChangeListener,它只会在值发生变化时被调用,而不是在最大或最小变化时被调用。
slider.getModel().addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
// do something here when value changes
});
});