0

Any one know how to bind one swing JComponent to two BeansBinding classes(Specially with Netbeans IDE)? Also how to bind a variable in JFrame to a beanbinding class's property?

divibisan
  • 11,659
  • 11
  • 40
  • 58
Débora
  • 5,816
  • 28
  • 99
  • 171
  • dont understand a) what exactly do you want to achieve in the first part b) what exactly is the problem in the second part (nothing special about frame properties, just bind them the same way as all others) – kleopatra Sep 21 '11 at 07:22
  • (a) Let's say there is a JTextField and it can be bound to a class called "BeanA"'s property: String name. At the same time I want to bind this JTextField to another class called "BeanB" 's property: String logger.In Netbeans IDE allows binding with only one Bean's property. – Débora Sep 21 '11 at 15:48
  • still don't understand: what do you expect the textfield to show/update, the name or the logger property? – kleopatra Sep 21 '11 at 15:53
  • (b) Let's say there is a int value (instance varaible)called attempts in a JFrame.How to bind this variable with Bean's property.(Now what I do is,I call a method in bean class relevant to the variable)Expect a solution how to bind an instance variable's value to bean's property. I am still new to Beansbinding and do all beansbinding with Netbeans IDE. – Débora Sep 21 '11 at 16:03
  • (a) Let's say the name property of the "BeanA" class. – Débora Sep 21 '11 at 16:06

1 Answers1

1

A) Hmm ... still not sure what exactly you want to achieve: build a binding chain, maybe? Something like

bean."name" <--> textField."text" --> otherBean.logger

    BindingGroup context = new BindingGroup();
    context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
            bean, BeanProperty.create("name"), 
            field, BeanProperty.create("text"))); 
    context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ,
            field, BeanProperty.create("text"), 
            otherBean, BeanProperty.create("logger"))); 

B) Beansbinding is all about binding properties not fields (aka: variables). So whatever you want to bind needs a getter and (maybe a setter, depends on your requirements) and must fire notification on change. Then do the binding just as always ..

public MyFrame extends JFrame {
    private int attempts;

    public int getAttempts() {
        return attempts;
    } 

    private void incrementAttempts() {
        int old = getAttempts();
        attempts++;
        firePropertyChange("attempts", old, getAttempts()); 
    }

    private void bind() {
    BindingGroup context = new BindingGroup();
    context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ,
            this, BeanProperty.create("attempts"), 
            label, BeanProperty.create("text"))); 

    }
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211