Java beanbinding JButton.enabled [英] java beansbinding JButton.enabled
问题描述
我正在使用NetBeans 7.3中的jdesktop的bean绑定库.我有一个非常具体的问题.如果另一个bean的任何属性都不为null,那么我想启用JButton;如果它为null,则希望禁用它.
I'm working with jdesktop's beansbinding library in Netbeans 7.3. I have a really specific question. I'd like to have a JButton enabled if any property of another bean is not null and disabled if it's null.
因此,我尝试创建一个ELBinding(具有条件支持,例如${myProperty > 50}
返回一个布尔值,该布尔值表示此表达式是否为真.
So I tried to create an ELBinding (which has conditional support like ${myProperty > 50}
returning a boolean holding whether this expression is true or not.
但是在我这种情况下,我无法弄清楚(也无法在互联网上找到)如何写下这种情况.如果我有一个用于属性更改的事件侦听器,则可以编写如下内容(在某些PropertyChangeListener实例的抽象方法中):
But in my occasion I cannot figure out (nor find on the internet) how to write down this condition. If I had an event listener for property changes, I'd write something like this (in some PropertyChangeListener instance's abstract method):
if (propertyChangeEvent.getNewValue() == null) {
button.setEnabled(false);
} else {
button.setEnabled(true);
}
非常感谢我提供的任何提示,因为我发现ELProperty的文献记载不充分.
Thanks a lot for any hints as I find ELProperties poorly documentated.
推荐答案
worksforme,请参见下面的示例.
worksforme, see the example below.
但是:通常,启用管理应该由bean本身来处理(相对于即时执行)-在一个设计良好的分离世界中,只有bean本身才应该拥有所有必要的知识.
But: usually enablement management shoud be handled by the bean itself (vs. doing so on the fly) - in a well-designed separated world, only the bean itself should have all the knowledge that's necessary.
某些代码:
final Person person = new Person();
// enablement binding with ad-hoc decision in view
Action action = new AbstractAction("Add year") {
public void actionPerformed(ActionEvent e) {
person.setAge(person.getAge() + 1);
}
};
JButton button = new JButton(action);
Binding enable = Bindings.createAutoBinding(UpdateStrategy.READ,
person, ELProperty.create("${age < 6}"),
button, BeanProperty.create("enabled"));
enable.bind();
// enablement binding to a bean property
Action increment = new AbstractAction("Increment year") {
public void actionPerformed(ActionEvent e) {
person.incrementAge();
}
};
JButton incrementButton = new JButton(increment);
Binding incrementable = Bindings.createAutoBinding(UpdateStrategy.READ,
person, BeanProperty.create("incrementable"),
incrementButton, BeanProperty.create("enabled"));
incrementable.bind();
JSlider age = new JSlider(0, 10, 0);
Binding binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
person, BeanProperty.create("age"),
age, BeanProperty.create("value"));
binding.bind();
// the bean
public static class Person extends AbstractBean {
private int age;
private int max;
public Person() {
max = 6;
}
public void incrementAge() {
setAge(getAge() + 1);
}
public boolean isIncrementable() {
return getAge() < max;
}
public void setAge(int age) {
boolean incrementable = isIncrementable();
int old = getAge();
this.age = age;
firePropertyChange("age", old, getAge());
firePropertyChange("incrementable", incrementable, isIncrementable());
}
public int getAge() {
return age;
}
}
这篇关于Java beanbinding JButton.enabled的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!