DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • JavaFX Gets Video Capabilities
  • The Challenges of a JavaFX Reboot
  • Calling JavaFX From Java?

Trending

  • Understanding and Mitigating IP Spoofing Attacks
  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments
  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  1. DZone
  2. Coding
  3. Java
  4. JavaFX Collection's ObservableList and ObservableMap

JavaFX Collection's ObservableList and ObservableMap

By 
Attune World Wide user avatar
Attune World Wide
·
Jul. 06, 15 · Interview
Likes (1)
Comment
Save
Tweet
Share
62.2K Views

Join the DZone community and get the full member experience.

Join For Free

Collections in JavaFX are defined by the javafx.collections package, which consists of the following interfaces and classes:

Interfaces:

  • ObservableList: A list that enables listeners to track changes when they occur

  • ListChangeListener: An interface that receives notifications of changes to an ObservableList

  • ObservableMap: A map that enables observers to track changes when they occur

  • MapChangeListener: An interface that receives notifications of changes to an ObservableMap


Classes:

  • FXCollections: A utility class that consists of static methods that are one-to-one copies of java.util.Collections methods

  • ListChangeListener.Change: Represents a change made to an ObservableList

  • MapChangeListener.Change: Represents a change made to an ObservableMap


Example of ObservableList

Here a standard List is first created. It is then wrapped with an ObservableList. A ListChangeListener is then registered, and will receive notification whenever a change is made on the ObservableList :

packageorg.attune.collection;

importjava.util.List;

importjava.util.ArrayList;

importjavafx.collections.ObservableList;

importjavafx.collections.ListChangeListener;

importjavafx.collections.FXCollections;

public class ObservableListDemo {

public static void main(String[] args)

{

List<String> list = new ArrayList<String>();


ObservableList<String>observableList = FXCollections.observableList(list);


observableList.addListener(new ListChangeListener() {

@Override

public void onChanged(ListChangeListener.Change change) {

System.out.println("Detected a change! ");

while (change.next()) {

System.out.println("Was added? " + change.wasAdded());

System.out.println("Was removed? " + change.wasRemoved());

}

}

});




observableList.add("a : item one");

System.out.println("Size: " + observableList.size()+observableList.toString());


list.add("d : item two");

System.out.println("Size: " + observableList.size()+observableList.toString());

observableList.add("f : item Three");

System.out.println("Size: " + observableList.size()+observableList.toString());


list.add("b : item four");

System.out.println("Size: " + observableList.size()+observableList.toString());


observableList.remove(1);

System.out.println("Size: " + observableList.size()+observableList.toString());


observableList.sort(null);

System.out.println("Size: " + observableList.size()+observableList.toString());


observableList.set(2, "c : item five");

System.out.println("Size: " + observableList.size()+observableList.toString());

}

}



Here is output of above program:

Detected a change!

Was added? true

Was removed? false

Size: 1[a : item one]

Size: 2[a : item one, d : item two]

Detected a change!

Was added? true

Was removed? false

Size: 3[a : item one, d : item two, f : item Three]

Size: 4[a : item one, d : item two, f : item Three, b : item four]

Detected a change!

Was added? false

Was removed? true

Size: 3[a : item one, f : item Three, b : item four]

Detected a change!

Was added? false

Was removed? false

Size: 3[a : item one, b : item four, f : item Three]

Detected a change!

Was added? true

Was removed? true

Size: 3[a : item one, b : item four, c : item five]


Example of ObservableMap

package org.attune.collection;
import java.util.Map;
import java.util.HashMap;
import javafx.collections.ObservableMap;
import javafx.collections.MapChangeListener;
import javafx.collections.FXCollections;

public class ObservableMapDemo {

public static void main(String[] args) {

Map<String,String> map = new HashMap<String,String>();

ObservableMap<String,String> observableMap = FXCollections.observableMap(map);
observableMap.addListener(new MapChangeListener() {
@Override
public void onChanged(MapChangeListener.Change change) {
System.out.println("Detected a change! ");
}
});

// Changes to the observableMap WILL be reported.
observableMap.put("key 1","value 1");
System.out.println("Size: "+observableMap.size() + observableMap.toString());

// Changes to the underlying map will NOT be reported.
map.put("key 2","value 2");
System.out.println("Size: "+observableMap.size()+ observableMap.toString());
// Changes to the observableMap WILL be reported.
observableMap.remove("key 1");
System.out.println("Size: "+observableMap.size() + observableMap.toString());
}
}

Here is the output:
Detected a change!
Size: 1{key 1=value 1}
Size: 2{key 2=value 2, key 1=value 1}
Detected a change!
Size: 1{key 2=value 2}


Stay tuned for More Educational Content Attune World Wide

JavaFX

Opinions expressed by DZone contributors are their own.

Related

  • JavaFX Gets Video Capabilities
  • The Challenges of a JavaFX Reboot
  • Calling JavaFX From Java?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!