binding in java beans
play

Binding in Java Beans Inspecting bean properties with a GUI - PowerPoint PPT Presentation

Binding in Java Beans HOM,DOS beans? convention Binding in Java Beans Inspecting bean properties with a GUI binding veto GUI BEANS Pieter van den Hombergh Summary on Java Beans FX Properties Thijs Dorssers Fontys Hogeschool voor


  1. Binding in Java Beans HOM,DOS beans? convention Binding in Java Beans Inspecting bean properties with a GUI binding veto GUI BEANS Pieter van den Hombergh Summary on Java Beans FX Properties Thijs Dorssers Fontys Hogeschool voor Techniek en Logistiek June 15, 2017 HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 1/18

  2. Binding in Java What are Java Beans Beans HOM,DOS Alternate meaning: coffee beans of the java sort. beans? In the java programming culture: Normal Java Classes convention that adhere to specific conventions: Inspecting bean properties with a GUI Must have public parameter-less (default) constructor. binding veto Other constructors are allowed. GUI BEANS Have setters and getters for properties with naming Summary on Java Beans convention. FX Properties Can have property change listeners with support for them. Must be serializable . They often group or encapsulate many objects into a single object. HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 2/18

  3. Binding in Java Naming convention Beans HOM,DOS All properties of the bean should have getters and setters. beans? For member named someValue of SomeType convention getter getSomeValue . First letter capitalized and prefixed Inspecting bean properties with a GUI binding with get for getter: SomeType getSomeValue() veto GUI BEANS setter setSomeValue . First letter capitalized and prefixed Summary on Java Beans with set for setter: void setSomeValue(SomeType v) FX Properties is for getter of boolean members: boolean isSomeBoolean() If these conventions are followed, it is said that the implementation follows the Bean Pattern. This naming convention eases understanding, but also allows for automated tools to introspect the classes and objects and help with tools for non programmatic (as in GUI supported) operations on the “bean”. DEMO in Netbeans HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 3/18

  4. Binding in Java Bean GUI in NetBeans Beans HOM,DOS DEMO with FaceBean. beans? Note that this version is slightly modified from the tutorial’s convention version, using the inherited property change support of Inspecting bean properties with a GUI java.awt.Component . binding veto GUI BEANS Summary on Java Beans FX Properties HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 4/18

  5. Binding in Java Binding is like Observer Beans HOM,DOS Binding means that a PropertyChangeListener is informed beans? whenever a property changes value. convention This is similar to the Observable/Observer pattern we Inspecting bean properties with a GUI binding saw earlier. veto PropertyChangeListener s can be added and GUI BEANS Summary on Java Beans removed. FX Properties The listeners are informed about the changes of the property by a call of their void propertyChange(PropertyChangeEvent evt) method. When a bean wants to use this mechanism it typically uses a member of type PropertyChangeSupport, which does the “Heavy Lifting”. In the FaceBean example mPcs . The listeners are triggered by the bean by calling mPcs.firePropertyChanged(propname, oldValue, newValue); HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 5/18

  6. Binding in Java Vetoable changes Beans HOM,DOS A constrained property gets special treatment. The beans has veto change listeners. beans? convention The listeners get a say if the change is allowed, based Inspecting bean properties with a GUI on name and old and new value of the property. binding veto The FaceBean example uses VetoableChangeSupport GUI BEANS instance name mVcs . Summary on Java Beans The way the listener is informed of the immanent FX Properties change is similar to firePropertyChange call on an earlier slide: fireVetoableChange(propname, oldValue, newValue) However: call this method before you change the properties value. Then when the listener vetoes the change, it will throw an exception, thereby typically preventing the execution of the steps to actually set the property to the vetoed value. This exception is checked , so you must catch it or forward it with a throws in the setter’s signature. HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 6/18

  7. Binding in Java Gui Components as beans Beans HOM,DOS java.awt.Component is a JavaBean and support change listening for the properties: beans? the component’s font (”font”) convention Inspecting bean properties background color (”background”) with a GUI binding foreground color (”foreground”) veto focusability (”focusable”) GUI BEANS focus traversal keys enabled state Summary on Java Beans (”focusTraversalKeysEnabled”) FX Properties Set of FORWARD TRAVERSAL KEYS (”forwardFocusTraversalKeys”) Set of BACKWARD TRAVERSAL KEYS (”backwardFocusTraversalKeys”) Set of UP CYCLE TRAVERSAL KEYS (”upCycleFocusTraversalKeys”) preferred size (”preferredSize”) minimum size (”minimumSize”) maximum size (”maximumSize”) locale (”locale”) component orientation (left to right or right to left) (”componentOrientation”) And you can add your own property too. HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 7/18

  8. Binding in Java Java Bean Summary Beans HOM,DOS A Java Bean is simple plain old java (as in: needs not beans? implement or extend anything) with some conventions. convention Non args ctor. Inspecting bean properties with a GUI binding Setters and getters name name the properties. veto GUI BEANS PropertyChangeListeners can be informed. (Name, Summary on Java Beans oldvalue, new value) FX Properties Changes can be vetoed. GUI (Swing, AWT) components are Java Beans. Auxiliary file (also using a name pattern) provides BeanInfo and a GUI based editor for the properties. HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 8/18

  9. Binding in Java FX Beans vs Java Beans Beans HOM,DOS more direct binding/ relationship of and between variables. beans? convention The work around triggering and binding does no longer Inspecting bean properties with a GUI take place in containing class. binding because the properties are “exposed”, so can veto GUI BEANS manipulated from the outside. Summary on Java Beans new (extra) convention for getter of property. FX Properties xXXXProperty() where xXXX is the name of the property. HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 9/18

  10. Binding in Java Meet Bill Beans HOM,DOS Simple bill example beans? convention p u b l i c c l a s s Bill { Inspecting bean properties with a GUI // Define a variable to store the property binding veto p r i v a t e amountDue = new ← DoubleProperty ֓ GUI BEANS SimpleDoubleProperty () ; Summary on Java Beans FX Properties // Define a getter for the property 's value p u b l i c f i n a l double getAmountDue () { r e t u r n amountDue . get () ; } // Define a setter for the property 's value p u b l i c f i n a l void setAmountDue ( double ) { value amountDue . set ( ) ; value } // Define a getter for the property itself p u b l i c amountDueProperty () { DoubleProperty r e t u r n amountDue ; } } HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 10/18

  11. Binding in Java Improvements Beans HOM,DOS JavaFX properties expands and improves the JavaBeans beans? model. convention Binding is assembled from one or more sources, the Inspecting bean properties with a GUI dependencies . binding veto A binding observes its dependencies for changes and GUI BEANS updates itself after change detection, but lazily. Summary on Java Beans FX Properties Lazy means, that the value is recomputed on demand (when getter is called) instead of every time it is set. The documentation also states that the implementer should minimize the work in event handlers: “Implementations of this class should strive to generate as few events as possible to avoid wasting too much time in event handlers.” So: if the new value is the same as the previous: do not bother. If you are already invalid, do not propagate this news any further. HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 11/18

  12. Binding in Java Property APIs Beans HOM,DOS JavaFX beans provide three way to deal with bound beans? properties: convention High level API. Inspecting bean properties with a GUI binding Fluent API veto Bindings utility class. GUI BEANS Summary on Java Beans Low level API. FX Properties HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 12/18

  13. Binding in Java High level API, Fluent Beans HOM,DOS Make use of the (computational) method in the property: beans? NumberProperty and its children. convention Inspecting bean properties with a GUI Fluent API binding veto p u b l i c s t a t i c void main ( String [ ] ) { args GUI BEANS num1 = new SimpleIntegerProperty ( 1 ) ; IntegerProperty Summary on Java Beans IntegerProperty num2 = new SimpleIntegerProperty ( 2 ) ; IntegerProperty num3 = new SimpleIntegerProperty ( 5 ) ; FX Properties NumberBinding sumMul = num1 . add ( num2 ) . multiply ( num3 ) ; System . out . println ( sumMul . getValue () ) ; num1 . set ( 2 ) ; System . err . println ( sumMul . getValue () ) ; } Note the use of chaining the method calls. It is because the add etc. methods return an object which support the operation. This is called a fluent style of programming. HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 13/18

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend