Beans Binding Framework & Swing Application Framework Geertjan Wielenga http//netbeans.dzone.com
2 Agenda ● Goals ● Beans Binding Framework ● Swing Application Framework ● Conclusion
3 ● Goals ● Beans Binding Framework ● Swing Application Framework ● Conclusion Agenda
4 Example
5
6
7
8 faceSlider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { face.setFaceStyle(faceSlider.getValue()); } }); face.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName() == “faceStyle”) { faceSlider.setValue(caricature.getFaceStyle()); } } });
9 bind(faceObject, eye, slider1, value); bind(faceObject, hair, slider2, value); bind(faceObject, face, slider3, value);
10
11 BindingGroup bindingGroup = new BindingGroup();
12 BindingGroup bindingGroup = new BindingGroup(); Binding binding1 = Bindings.createAutoBinding( AutoBinding.UpdateStrategy.READ_WRITE, field, ELProperty.create("${text}"), slider, BeanProperty.create("value"));
13 BindingGroup bindingGroup = new BindingGroup(); Binding binding1 = Bindings.createAutoBinding( AutoBinding.UpdateStrategy.READ_WRITE, field, ELProperty.create("${text}"), slider, BeanProperty.create("value")); bindingGroup.addBinding(binding1); bindingGroup.bind();
14 Demo JFrame in Plain Java Class
15 validators & converters
16 Sample in the IDE
17 Summary ● Different update strategies ● Read once, read only from source, keep source and target in sync ● Ability to do validation ● Ability to transform value ● String to Color, Date to String
18 Demo Beans Binding Framework with “Matisse” GUI Builder
19 Latest Developments ● Expression language is no longer mandatory ● Subclasses for complex cases ● JTable binding ● Column binding ● JComboBox binding ● JList binding
20 ● When will Beans Binding be in the JDK? ● Planned - Java Platform 7 ● I can't wait. Where can I get it? ● Version ● Released November 2, 2007 ● Available at ● Bundled in NetBeans 6.0 When and Where?
21 ● Goals ● Beans Binding Framework ● Swing Application Framework ● Conclusion Agenda
22 public static void main(String args[]) { // good luck! } Problem Statement
23 ● Goals: ● As small as possible ● Explain it all in an hour ● Work well for small/medium apps ● Nice consequence: toolable Framework Goals
24 ● Lifecycle support ● Resources ● Actions ● Tasks ● Session state Framework Features
25 ● Modular system ● Docking system ● File system ● Data model ● Help system ● Update mechanism Out of Scope
26 Framework Architecture
27 import javax.swing.JFrame; import javax.swing.JLabel; public class HelloWorldSwing { public static void main(String[] args) { JFrame frame = new JFrame("HelloWorldSwing"); final JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } Lifecycle Support (On your own)
28 import javax.swing.JLabel; import org.jdesktop.application.Application; import org.jdesktop.application.SingleFrameApplication; public class MyApp extends SingleFrameApplication protected void startup() { JLabel label = new JLabel("Hello World"); show(label); } public static void main(String[] args) { Application.launch(MyApp.class, args); } Lifecycle Support
29 Lifecycle Support Calls initialize(), startup(), ready() on EDT thread. Performs initializations that must happen before the GUI is created like setting look & feel. Creates the initial GUI and shows it. All applications will override this method. Any work that must wait until the GUI is visible and ready for input. Calls shutdown() if the exitListeners do not veto. Main frame's Windowlistener calls exit(). Take the GUI down and do final cleanup.
30 ● Defined with regular ResourceBundles ● ResourceManager class is used ● Three options: ● Manually load, use ResourceMap objects ● Automatically inject resources into UI components ● Automatically inject resources into object fields Resources
31 ● Instead of ActionListeners ● Asynchronous actions are easy annotation ● Possible simple = "changesPending") public void save() {... } public boolean getChangesPending() {... } Actions
32 // define sayHello Action – pops up message public void sayHello() { String s = textField.getText(); JOptionPane.showMessageDialog(s); } // use sayHello – set the action property Action sayHello = Application.getContext(). getActionMap(MyForm.class, this).get("sayHello"); textField.setAction(sayHello); button.setAction(sayHello); Actions
33 ● Inherit the SwingWorker API ● Support for monitoring ● Title, Start/Done, etc. ● return Tasks ● Easy to handle non-blocking actions Tasks
34 ● Easily save properties: ● Window size ● Application position ● Sizes of columns in jTable, etc. ● ApplicationContext.getSessionStorage() ● SessionStorage.save(RootComponent, filename) ● Default for SingleFrameApplication is windowname + “.session.xml” ● Uses XMLEncoder & XMLDecoder Session State
35 Demo Flickr Search Application
36 ● NetBeans Platform is for applications that need more: ● Window system ● File/Data systems ● Help system ● Update system ● Visual designer ● Explorer widgets ● etc.... JSR 296 vs. NB Platform
37 ● When will JSR-296 be in a JDK? ● Planned - Java Platform 7 ● I can't wait. Where can I get it? ● Version 1.0 packaged with NetBeans 6 ● Source available as a NetBeans project at When and Where?
38 Conclusion ● JSR 295 and 296 are making Swing development fun again ● Both are very well supported in NetBeans GUI builder (project Matisse) ● Desktop Java becoming interesting for “Delphi” and “VB” developers ● Desktop Java is well and alive!