简体   繁体   中英

java beansbinding JButton.enabled

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM