Добавить
Уведомления

Understanding Binding Support in JavaBeans, Learn JavaFX

Playlist https://www.youtube.com/playlist?list=PLIpLw6v7Z1qla5xBkQlFmM1h5EAiivVoi 2023 10 12 05 22 27 Understanding Binding Support in JavaBeans Before I discuss JavaFX properties and binding, let’s take a short tour of binding support in the JavaBeans API. You may skip this section if you have used the JavaBeans API before. Java has supported binding of bean properties since its early releases. Listing 2-1 shows an Employee bean with two properties, name and salary. Listing 2-1. An Employee Java Bean with Two Properties Named name and salary // Employee.java package com.jdojo.binding; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; public class Employee { private String name; private double salary; private PropertyChangeSupport pcs = new PropertyChangeSupport(this); 26 Chapter 2 ■ Properties and Bindings public Employee() { this.name = "John Doe"; this.salary = 1000.0; } public Employee(String name, double salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double newSalary) { double oldSalary = this.salary; this.salary = newSalary; // Notify the registered listeners about the change pcs.firePropertyChange("salary", oldSalary, newSalary); } public void addPropertyChangeListener( PropertyChangeListener listener) { pcs.addPropertyChangeListener(listener); } public void removePropertyChangeListener( PropertyChangeListener listener) { pcs.removePropertyChangeListener(listener); } @Override public String toString() { return "name = " + name + ", salary = " + salary; } } Both properties of the Employee bean are read/write. The salary property is also a bound property. Its setter generates property change notifications when the salary changes. Interested listeners can register or deregister for the change notifications using the addPropertyChangeListener() and removePropertyChangeListener() methods. The PropertyChangeSupport class is part of the JavaBeans API that facilitates the registration and removal of property change listeners and firing of the property change notifications. 27 Chapter 2 ■ Properties and Bindings Any party interested in synchronizing values based on the salary change will need to register with the Employee bean and take necessary actions when it is notified of the change. Listing 2-2 shows how to register for salary change notifications for an Employee bean. The output below it shows that salary change notification is fired only twice, whereas the setSalary() method is called three times. This is true because the second call to the setSalary() method uses the same salary amount as the first call, and the PropertyChangeSupport class is smart enough to detect that. The example also shows how you would bind variables using the JavaBeans API. The tax for an employee is computed based on a tax percentage. In the JavaBeans API, property change notifications are used to bind the variables. Listing 2-2. An EmployeeTest Class That Tests the Employee Bean for Salary Changes // EmployeeTest.java package com.jdojo.binding; import java.beans.PropertyChangeEvent; public class EmployeeTest { public static void main(String[] args) { final Employee e1 = new Employee("John Jacobs", 2000.0); // Compute the tax computeTax(e1.getSalary()); // Add a property change listener to e1 e1.addPropertyChangeListener( EmployeeTest::handlePropertyChange); // Change the salary e1.setSalary(3000.00); e1.setSalary(3000.00); // No change notification is sent. e1.setSalary(6000.00); } public static void handlePropertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); if ("salary".equals(propertyName)) { System.out.print("Salary has changed. "); System.out.print("Old:" + e.getOldValue()); System.out.println(", New:" + e.getNewValue()); computeTax((Double)e.getNewValue()); } } public static void computeTax(double salary) { final double TAX_PERCENT = 20.0; double tax = salary * TAX_PERCENT/100.0; System.out.println("Salary:" + salary + ", Tax:" + tax); } } 28 Chapter 2 ■ Properties and Bindings Salary:2000.0, Tax:400.0 Salary has changed. Old:2000.0, New:3000.0 Salary:3000.0, Tax:600.0 Salary has changed. Old:3000.0, New:6000.0 Salary:6000.0, Tax:1200.0

12+
16 просмотров
2 года назад
12+
16 просмотров
2 года назад

Playlist https://www.youtube.com/playlist?list=PLIpLw6v7Z1qla5xBkQlFmM1h5EAiivVoi 2023 10 12 05 22 27 Understanding Binding Support in JavaBeans Before I discuss JavaFX properties and binding, let’s take a short tour of binding support in the JavaBeans API. You may skip this section if you have used the JavaBeans API before. Java has supported binding of bean properties since its early releases. Listing 2-1 shows an Employee bean with two properties, name and salary. Listing 2-1. An Employee Java Bean with Two Properties Named name and salary // Employee.java package com.jdojo.binding; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; public class Employee { private String name; private double salary; private PropertyChangeSupport pcs = new PropertyChangeSupport(this); 26 Chapter 2 ■ Properties and Bindings public Employee() { this.name = "John Doe"; this.salary = 1000.0; } public Employee(String name, double salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double newSalary) { double oldSalary = this.salary; this.salary = newSalary; // Notify the registered listeners about the change pcs.firePropertyChange("salary", oldSalary, newSalary); } public void addPropertyChangeListener( PropertyChangeListener listener) { pcs.addPropertyChangeListener(listener); } public void removePropertyChangeListener( PropertyChangeListener listener) { pcs.removePropertyChangeListener(listener); } @Override public String toString() { return "name = " + name + ", salary = " + salary; } } Both properties of the Employee bean are read/write. The salary property is also a bound property. Its setter generates property change notifications when the salary changes. Interested listeners can register or deregister for the change notifications using the addPropertyChangeListener() and removePropertyChangeListener() methods. The PropertyChangeSupport class is part of the JavaBeans API that facilitates the registration and removal of property change listeners and firing of the property change notifications. 27 Chapter 2 ■ Properties and Bindings Any party interested in synchronizing values based on the salary change will need to register with the Employee bean and take necessary actions when it is notified of the change. Listing 2-2 shows how to register for salary change notifications for an Employee bean. The output below it shows that salary change notification is fired only twice, whereas the setSalary() method is called three times. This is true because the second call to the setSalary() method uses the same salary amount as the first call, and the PropertyChangeSupport class is smart enough to detect that. The example also shows how you would bind variables using the JavaBeans API. The tax for an employee is computed based on a tax percentage. In the JavaBeans API, property change notifications are used to bind the variables. Listing 2-2. An EmployeeTest Class That Tests the Employee Bean for Salary Changes // EmployeeTest.java package com.jdojo.binding; import java.beans.PropertyChangeEvent; public class EmployeeTest { public static void main(String[] args) { final Employee e1 = new Employee("John Jacobs", 2000.0); // Compute the tax computeTax(e1.getSalary()); // Add a property change listener to e1 e1.addPropertyChangeListener( EmployeeTest::handlePropertyChange); // Change the salary e1.setSalary(3000.00); e1.setSalary(3000.00); // No change notification is sent. e1.setSalary(6000.00); } public static void handlePropertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); if ("salary".equals(propertyName)) { System.out.print("Salary has changed. "); System.out.print("Old:" + e.getOldValue()); System.out.println(", New:" + e.getNewValue()); computeTax((Double)e.getNewValue()); } } public static void computeTax(double salary) { final double TAX_PERCENT = 20.0; double tax = salary * TAX_PERCENT/100.0; System.out.println("Salary:" + salary + ", Tax:" + tax); } } 28 Chapter 2 ■ Properties and Bindings Salary:2000.0, Tax:400.0 Salary has changed. Old:2000.0, New:3000.0 Salary:3000.0, Tax:600.0 Salary has changed. Old:3000.0, New:6000.0 Salary:6000.0, Tax:1200.0

, чтобы оставлять комментарии