Constructing reactive system with JavaFX tool _java

Source: Internet
Author: User
Tags readable

JavaFX is a new standard library for building graphical applications in Java, but many programmers still insist on using swing and even AWT. Here are some tips on how to build responsive, fast applications with new awesome features in the JavaFX tool set!

1. Property value

If you have done a complete understanding of the JavaFX component, the move has encountered the property this thing. Almost every value in the FX library can be observed, the width of the partition (divider), the size of the picture, the text in the textual Identification (label), the Children in a list, and the status of the checkbox. Attributes are divided into alternative: writable properties and readable properties. Writable values can be modified using the Setup method or directly modified. JavaFX handles the event disposal process and ensures that each component that relies on this property is notified. A readable property has a method that allows you to receive notification when its value is modified.

Example:

Readable-and writable
stringproperty name = new Simplestringproperty ("Emil"); 
Read-only
observablebooleanvalue nameisempty = Name.isempty ();

2. Bound value

When you have a writable and readable value, you can begin to define rules about how these values are associated. A writable property can be bound to a readable property so that its value always matches to the readable one. Bindings do not occur immediately, but they take place before the values are observed (see what I have done there). Bindings can be one-way or bidirectional. Of course, if they are bidirectional, two attributes are required to be writable.

Example:

TextField Fielda = new TextField ();
TextField fieldb = new TextField ();
Fielda.prefwidthproperty (). Bind (Fieldb.widthproperty ());

3. An observable list

Attributes are not the only things that can be observed. If the list is encapsulated in a observablelist, then the members of the list can also be observed. The response model for Observablelist is quite advanced. You will not only be notified when the list is modified, but you can also see how the list is modified.

Example:

list<string> otherlist = arrays.aslist ("foo", "Bar", "Bar");
observablelist<string> list = Fxcollections.observablelist (otherlist);

List.addlistener (listchangelistener.change<? extends string> change)-> {
  System.out.println (" Received event. ");
  while (Change.next ()) {
    if (change.wasadded ()) {
      System.out.println (
        "Items" + change.getaddedsublist () + "was added.");
    }

    if (change.wasremoved ()) {
      System.out.println (
        "Items" + change.getremoved () + "was removed.");
    }
  }
});

System.out.println ("old list:" + list);
List.set (1, "foo");
System.out.println ("New list:" + list);

The running output of the above code is as follows:

Old list: [foo, bar, bar]
Received event.
Items [foo] was added.
Items [bar] was removed.
New list: [foo, foo, bar]

As you can see, the setup operation triggers only one event.

4. Stringconverter
Sometimes you will find yourself creating a binding without having to extract the values from a component. A typical example of this is that you have a stringproperty with path that you get from a text field (TextField). If you want to have an observable attribute with this value represented as path, you need to create a stringconverter for it.
Example:

TextField filelocation = new TextField ();
Stringproperty location = Filelocation.textproperty ();
property<path> Path = new simpleobjectproperty<> ();
 
Bindings.bindbidirectional (location, path, new stringconverter<path> () {
  @Override public
  String ToString (path Path) {return
    path.tostring ();
  }
 
  @Override public
  Path fromstring (String string) {return
    paths.get (string);
  }
);

An object property is not a two-way binding to the value of a text field.

5. Expressions

Using the bindings class above, you can create any type of expression. For example, you have two text fields that allow users to enter information. Now you want to define a read-only domain, which always contains a string, and if the length of two strings is equal, it will be displayed with two strings being mixed together at character intervals. If the length is unequal, a help message is displayed.
Example:

TextField-New TextField ();
TextField second = new TextField ();
TextField Mix  = new TextField ();
 
Mix.textproperty (). Bind (
  bindings.when (
    first.lengthproperty (). Isequalto (Second.lengthproperty ()
  ) ). Then (Bindings.createstringbinding ()
    -> {
      int length    = First.lengthproperty (). get ();
      String Firsttext = First.textproperty (). get ();
      String Secondtext = Second.textproperty (). get ();
      Char[] Result   = new char[length * 2];
 
      for (int i = 0; i < length; i++) {
        result[i * 2]   = Firsttext.charat (i);
        Result[i * 2 + 1] = Secondtext.charat (i);
      }
 
      Return a new String (result);
    }, 
    First.textproperty (),
    Second.textproperty ())
  . otherwise (" Please enter two strings of exactly the same length. ")
);

Here are just a few of the many features of JavaFX. Hopefully you'll find more creative ways to take advantage of this event system!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.