A Book Class with a Read-Only and Two Read-Write Properties - Java JavaFX

Java examples for JavaFX:Bindable Properties

Description

A Book Class with a Read-Only and Two Read-Write Properties

Demo Code

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ReadOnlyProperty;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

class Book {
  private StringProperty title = new SimpleStringProperty(this, "title",
      "Unknown");
  private DoubleProperty price = new SimpleDoubleProperty(this, "price", 0.0);
  private ReadOnlyStringWrapper ISBN = new ReadOnlyStringWrapper(this, "ISBN",
      "Unknown");

  public Book() {
  }/* w  w w.ja va 2s.  com*/

  public Book(String title, double price, String ISBN) {
    this.title.set(title);
    this.price.set(price);
    this.ISBN.set(ISBN);
  }

  public final String getTitle() {
    return title.get();
  }

  public final void setTitle(String title) {
    this.title.set(title);
  }

  public final StringProperty titleProperty() {
    return title;
  }

  public final double getprice() {
    return price.get();
  }

  public final void setPrice(double price) {
    this.price.set(price);
  }

  public final DoubleProperty priceProperty() {
    return price;
  }

  public final String getISBN() {
    return ISBN.get();
  }

  public final ReadOnlyStringProperty ISBNProperty() {
    return ISBN.getReadOnlyProperty();
  }
}

public class Main {
  public static void main(String[] args) {
    Book book = new Book("java2s.com", 49.99, "1234567900");
    System.out.println("After creating the Book object...");
    // Print Property details
    printDetails(book.titleProperty());
    printDetails(book.priceProperty());
    printDetails(book.ISBNProperty());
    // Change the book's properties
    book.setTitle("java2s.com examples");
    book.setPrice(9.49);

    System.out.println("\nAfter changing the Book properties...");

    // Print Property details
    printDetails(book.titleProperty());
    printDetails(book.priceProperty());
    printDetails(book.ISBNProperty());
  }

  public static void printDetails(ReadOnlyProperty<?> p) {
    String name = p.getName();
    Object value = p.getValue();
    Object bean = p.getBean();
    String beanClassName = (bean == null) ? "null" : bean.getClass()
        .getSimpleName();
    String propClassName = p.getClass().getSimpleName();

    System.out.print(propClassName);
    System.out.print("[Name:" + name);
    System.out.print(", Bean Class:" + beanClassName);
    System.out.println(", Value:" + value + "]");
  }
}

Related Tutorials