X

Geertjan's Blog

  • February 10, 2008

Beans Binding for Dummies

Geertjan Wielenga
Product Manager
If you want to really understand "beans binding", i.e., JSR-295, you can study lots of documents. Alternatively, you can go through a small but very powerful demo, here. At the end, you have a bean, which has properties that you bind to Swing components. No coding at all, because the NetBeans tooling makes it a point-and-click experience.

At the end, and only a few clicks later, I moved the demo to the NetBeans Platform, installing it in the IDE's "output" window position. (I created a suite, which included two library wrappers, for the beans binding JAR and the original app JAR, together with my functionality module that provides the TopComponent, which creates the new window in the IDE.) Now, I can change the text in the JTextField below and then immediately the text in the ticker is changed. Why? Because the relevant property in the ticker bean is bound to the Swing component (in real life, when you run the app, the yellow banner with the blue text is moving from right to left, like a "ticker", such as is found on the bottom of the screen on CNN, for example):

Take my advice and follow the Ticker Demo and you too will find out what it's all about.

Join the discussion

Comments ( 11 )
  • Mark Ashworth Sunday, February 10, 2008

    Hi,

    I would just like to know if there is a demo or tutorial on how to use binding with List and JTable.

    - Mark


  • Geertjan Monday, February 11, 2008
  • Mark Ashworth Monday, February 11, 2008

    Good Day,

    Thank you for your link. I should have specified my needs a bit better.

    I have a List to JTable binding but I want to implement the following features:

    1. A button that adds an element to the List which should cause the JTable to show a new row but I am not sure of the property change event that I should raise on the listeners for this to work.

    2. When I select a row in the JTable I need some property on the form to get that a row was selected. The link above mentions that it is possible but does not describe how.

    Another off the beat question is why the following binding does not work:-

    Source: Form

    EL: ${company.address}

    but if I create a bean on the form for the Company class called company1 and I change the init code as follows:

    Company company1 = new Company(); //Can't change

    company1 = getCompany(); //Pre-init

    Source: company1

    El: ${address}

    then it works. It should evaluate to the same thing. Also not sure why the construction of the company1 variable does not allow me to use the getComapny() method to initialise it.

    Thanks,

    Mark P Ashworth


  • Geertjan Monday, February 11, 2008

    Mark, I think the next blog entry, called "Beans Binding Via the Road Less Travelled By", answers several of your questions. If not, please feel free to ask again.


  • Mark Ashworth Monday, February 11, 2008

    Hi,

    Thank you for the road less travelled tutorial.

    For beans binding to see that an object was added to a List<Department> I found that the following worked but I am not to sure if it is optimal.

    public void setDepartments(List<Department> departments) {

    List<Department> old = getDepartments();

    this.departments = departments;

    firePropertyChange("departments", old, getDepartments());

    }

    public boolean add(Department department) {

    List<Department> d = new ArrayList<Department>(getDepartments());

    boolean result = d.add(department);

    setDepartments(d);

    return result;

    }


  • Geertjan Tuesday, February 12, 2008

    Hi Mark, does the next part in the series help you?


  • Pixy Wednesday, February 13, 2008

    Hi, thanks for the demo! It was very helpful and interesting although it's small.


  • Celal Krutz Wednesday, February 13, 2008

    Have you ever created a mobile game in netbeans before? could give us a complete sample including source codes,plug-ins,sprites and etc...


  • Richard Kennard Wednesday, February 13, 2008

    Hi Geertjan,

    Nice blog entry.

    You may be interested in a new Open Source project I've just released which helps automate the use of Beans Binding:

    http://www.metawidget.org

    If you have the time, your feedback would be very helpful to me.

    Regards,

    Richard


  • Allen Parslow Thursday, March 27, 2008

    I would like to see more examples of binding a pojo-sytle model to a swing view via beans-binding jsr 295 (as this is by far the major use-case of beans binding).


  • Aljoscha Rittner Saturday, January 24, 2009

    @Allen Parslow:

    The ClassBuilder project use annotations and code injection to extend pojos with cumbersome fire\*-methods. It's a small project, functional, but in alpha stage.

    http://code.google.com/p/javabuilders/wiki/ClassBuilder

    best regards, josh.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.