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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Top 7 Mistakes When Testing JavaFX Applications
  • JavaFX Gets Video Capabilities
  • The Challenges of a JavaFX Reboot
  • Calling JavaFX From Java?

Trending

  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • Why Good Models Fail After Deployment
  • Can Claude Skills Replace Playwright Agents? A Practical View for QA Engineers
  • Working With Cowork: Don’t Be Confused
  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.8K 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

  • Top 7 Mistakes When Testing JavaFX Applications
  • JavaFX Gets Video Capabilities
  • The Challenges of a JavaFX Reboot
  • Calling JavaFX From Java?

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook