Hello BeansBinding on the NetBeans Platform
Without typing a single line of code, you can integrate JSR 295, the Beans Binding Framework, into a NetBeans Platform application. (Whether you want to do so is a separate question, now that this JSR will apparently not be in JDK 7, while JavaFX contains something similar [and even more effective than JSR-295, in terms of simplicity].) Nevertheless, ignoring those two details for the moment, let's see how easy it is to "implement" Beans Binding on the NetBeans Platform.
- Create a new NetBeans module. In my case, I called the module "BBDemo", with code name base "org.netbeans.bbdemo". No need to let the wizard create a "layer.xml", because we will create a new TopComponent and , in doing so, the New Window Component wizard will create that file for us.
- Create a new TopComponent, after stepping through the New Window Component wizard. Do this by right-clicking the project node that is created at the end of the previous step, and then choosing New File. In the New File dialog, choose Module Development | Window Component. Accept all the defaults, setting "Demo" as the class name prefix.
- Drag and drop a JSlider onto the TopComponent. Do this from the Palette, which you can open by clicking Ctrl-Shift-8.
- In the same way, drag and drop a JTextField above the JSlider on the TopComponent. Resize the TopComponent a bit to make everything fit snugly, with this result:
- Now we'll connect them without the tedious listener-code that is typically required for this purpose. Right-click the JTextField and choose Bind | Text. In the dialog that appears, choose "jSlider1" in "Binding Source", which will enable you to choose "value" in "Binding Expression". You should now see this:
- Right-click the module, choose Properties, and go to the Libraries tab. There, click Add, type 'binding', and then you will see the Beans Binding integration module filtered at the top of the list. Yes, the Beans Binding JAR is just one of the many JARs available to you in the form of modules in the NetBeans module system. Click OK and then OK again to confirm that you want to use this library.
Notice that the JTextField is filled with the default value of the JSlider, indicating that the two Swing components are connected. Run the module (right-click the module and choose Run). The IDE will start a new NetBeans IDE instance and you will see your new window with the two Swing components connected. In other words, change something in the JSlider and the JTextField will automatically be updated:
No listeners have been set, only beans bindings between the two components. Study the generated code (in the Source view of the TopComponent) to see what's going on.
Hurray, done. Now wasn't that easy? Next time we'll apply these principles to the database scenario outlined in yesterday's "Hello EclipseLink on the NetBeans Platform" article.