java beansbinding JButton.enabled

时光总嘲笑我的痴心妄想 提交于 2020-01-15 23:42:03

问题


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.

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.

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

Thanks a lot for any hints as I find ELProperties poorly documentated.


回答1:


worksforme, see the example below.

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.

Some code:

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


来源:https://stackoverflow.com/questions/17930500/java-beansbinding-jbutton-enabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!