An analysis of 1.Scene graph system structure
JAVAFX organizes nodes in the form of a tree, with each node being a control, the UI component.
node is divided into leaf node and branch node, root node.
The most critical class in the scene system:
Scene: Represents the top-level container that contains all UI components
Node: is an abstract class that represents the base class of the UI component
Parent: is an abstract class that represents the base class for branch node.
The property mechanism of 2.Properties and Bindingjavafx is based on JavaBean, but it has been extended to some extent compared with JavaBean. The property of JavaFX is a wrapper for the Java primitive type. and the embedded binding mechanism, we can bind multiple listeners to this property, and the property is responsible for notifying them. The second understands the property specification in JavaFX: Final,property suffix, get,set and external interaction are Java primitive types.
Package Propertydemo;import Javafx.beans.property.doubleproperty;import Javafx.beans.property.SimpleDoubleProperty; Class Bill { //Define a variable to store the property private Doubleproperty amountdue = new Simpledoublepropert Y (); Define a getter for the property's value public final double getamountdue () {return amountdue.get ();} Define a setter for the property's value public final void Setamountdue (double value) {Amountdue.set (value);} Define a getter for the property itself public Doubleproperty Amountdueproperty () {return amountdue;}}
Understand the monitoring mechanism:
Bill Electricbill = new bill (); Electricbill.amountdueproperty (). AddListener (New ChangeListener () { @Override public void Changed ( Observablevalue o,object oldval, Object newval) { System.out.println ("Electric Bill has changed!"); } ); electricbill.setamountdue (100.00);
The Bindings property is dependent on the bindings class, and the property changes affect its bindings:
Integerproperty NUM1 = new Simpleintegerproperty (1); Integerproperty num2 = new Simpleintegerproperty (2); Numberbinding sum = Num1.add (num2); It can also be this way: numberbinding sum = Bindings.add (num1,num2); System.out.println (Sum.getvalue ()); Num1.set (2); System.out.println (Sum.getvalue ());
Observable,observablevalue,invacationlistener,changelistener
The observable and Observablevalue interface are responsible for fire triggering property change notifications. Observable Trigger Invacationlistener, and Observablevalue is responsible for triggering changelistener. We know that almost all the classes in the Javafx.beans.property package implement the Observablevalue interface, and the Observablevalue interface is observable Sub-interface. So each property class is also a observable (Value). A few points to note:Observable supports lazy compute to submit. That is, the calculation is not immediately submitted after the change of the value, and is compute only when needed.Therefore, when the value changes, the property will be in the invalid state, will trigger the Invacationlistener.But if your Observablevalue object, add the ChangeListener, this lazy compute will fail.
numberbinding total = Bindings.add (Bill1.amountdueproperty (). Add (Bill2.amountdueproperty ()), Bill3.amountdueproperty ()); Total.addlistener (New Invalidationlistener () { @Override public void invalidated (Observable o) { System.out.println ("The binding is now invalid."); } ); First Call makes the binding invalid bill1.setamountdue (200.00); The binding is now invalid bill2.setamountdue (100.00); Bill3.setamountdue (75.00); Make the binding valid ... System.out.println (Total.getvalue ());
Custom bindings: When implementing a custom bindings we need to Super.bind () in the constructor and overwrite the Computevalue () of the parent class to return the current value. You don't have to judge if invalid, the parent will do it for you.
Final Doubleproperty a = new Simpledoubleproperty (1); Final Doubleproperty B = new Simpledoubleproperty (2); Final Doubleproperty C = new Simpledoubleproperty (3); Final Doubleproperty d = new Simpledoubleproperty (4); Doublebinding db = new doublebinding () { { super.bind (A, B, C, D); } @Override protected Double computevalue () { return (A.get () * B.get ()) + (C.get () * D.get ()); } ; System.out.println (Db.get ()); B.set (3);
3.javaFx Collections
JavaFX collections is based on the collection framework in the Java base Class library, but combines observable in JavaFX to make it a collection of observable changes. Easy model data maintenance for JavaFX.
Main classes in the Javafx.collections package:
Interface:
Observablelist, Observablemap: set by listener
Listchangelistener mapchangelistener Listener Collection
Class:
Fxcollections: It is a 1:1 replica based on the java.util.Collections. It's just a layer of javafx localization. But it is the most important class, and all Observablelist/map are produced by its factory method.
Listchangelistener.change Mapchangelistener.change: All changes in the inclusion list or map. You can perform an iterative operation.
The change class contains the following types of changes: Sort changes, add delete changes, update changes. and has the corresponding judgment method.
Use Java collections to create the List. list<string> list = new arraylist<string> (); Now add observability by wrapping it with observablelist. observablelist<string> observablelist = fxcollections.observablelist (list); Observablelist.addlistener (New Listchangelistener () { @Override public void OnChanged ( Listchangelistener.change change) { System.out.println ("detected a change!"); } ); Changes to the observablelist would be reported. This line would print out "detected a change!" Observablelist.add ("Item One");
This code would work with any of the previous Observablelist Examplesobservablelist.addlistener (new Listchangelistener () {@Overridepublic void onChanged (Listchangelistener.change change) { System.out.println ("detected a change!"); while (Change.next ()) { System.out.println ("is added?" + change.wasadded ()); System.out.println ("was removed?" + change.wasremoved ()); System.out.println ("was replaced?" + change.wasreplaced ()); System.out.println ("was permutated?" + change.waspermutated ());}} );
Observablelist thelist = ...; Thelist.addlistener (New listchangelistener<item> () {public void onChanged (change<tem> c) { while (C.next ()) { if (c.waspermutated ())} {for (int i = C.getfrom (); I < C.getto (); ++i) { //permutate
}} else if (c.wasupdated ()) { //update item } else {for (item remitem:c.getremoved ()) { Remitem. Remove (outer.this); } For (Item additem:c.getaddedsublist ()) { additem.add (outer.this); }
However, you should note that the operations on these collections are not thread-safe. So you need to ensure thread safety when necessary.
An analysis of JavaFX application logic