
Quick Contact
Java Tutorial
- What is Java?
- History of Java
- Hello Java Program
- Features of Java
- Basic Syntax
- Java Setup
- Data Types in Java
- Java Variables
- Operators in Java
- JVM
- Java If-else Statement
- Switch Case Statement
- Java Loops
- Do-While Loop
- Java While Loop
- Continue Statement
- Break Statement in Java
- Constructors in Java
- Oops Concept in Java
- Features of OOPs
- Inheritance
- Exeception handeling
- Aggregation (HAS-A relationship) in Java
- Method Overriding in Java
- Method Overloading
- Java Static Keyword
- Java This Keyword
- Java Final Keyword
- Polymorphism
- Static Binding and Dynamic Binding
- Abstract class in Java
- Access Modifiers in Java
- Difference between abstract class and interface
- Interface in Java
- Garbage Collection in Java
- Java Package
- Encapsulation
- Serialization and Deserialization in Java
- Java Inner Classes
- Java Applets
- Multithreading in Java
- Thread Priorities in Java
- Thread Creation
- Inter Thread Communication
- Wrapper Classes in Java
- Java Input Output
- Java AWT Introduction
- Java Layout Manager
- Java Layout Policy
- Java AWT Events
- Collection Framework
- Collection Framework List Interface
- Swing in Java
- Swing Utility Classes
- Swing Layout Managers
- Java JDBC
- Hibernate Framework Overview – Architecture and Basics
Springboot
- Spring Environment Setup
- Spring Boot CRUD REST API Project using IntelliJ IDEA | Postman | MySQL
- Dockerizing Spring Boot Application | Spring Boot Docker Tutorial
- spring-boot-restapidocumentation with swagger
- Spring Boot HttpClient Overview
- Apache HttpClient POST HTTP Request Example
- Apache HttpClient PUT HTTP Request Example
- Apache HttpClient DELETE HTTP Request Example
- Apache HttpClient HTML Form POST Request Example
- Spring Boot JSP Exampl
- Deploying Spring Boot WAR file with JSP to Tomcat
- Spring Boot Annotations
- Spring Core Annotations
- Spring MVC Annotations with Examples
- Spring Scheduling Annotations
- Spring - Java-based Container Configuration
- Spring Java Based Configuration Example
Hibernate
- Hibernate 5 hello world
- Hibernate- One to One Unidirectional Mapping Annotation Example
- Hibernate - Batch Processing
- Hibernate - Interceptors
- Hibernate 5 - Create, Read, Update and Delete (CRUD) Operations Example
- Hibernate Transaction Management
- Hibernate One to Many Unidirectional Mapping Example
- Hibernate One to Many Bidirectional Mapping Example
- Hibernate Many to Many Annotation Mapping Example
- Hibernate Primary KeyJoin Column
- Hibernate First Level Cache with Example
- Hibernate XML Configuration Example with Maven + Eclipse + MySQL Database
- Hibernate Java Configuration Example
- JPA 2 with Hibernate 5 Bootstrapping Example
- JPA and Hibernate Cascade Types
- Hibernate/JPA - Primary Key Generation
- Hibernate 5 - Enum Type Mapping Example
- Hibernate Component Mapping
- Hibernate Object States – Transient,Persistent and Detached
- Hibernate 5 - Save an Entity Example
- Hibernate 5 - Persist an Entity Example
- Hibernate 5 - saveOrUpdate() Method Example
- Hibernate 5 - get(), load() and byId() Method Examples
- Hibernate 5 - merge() Example
- Hibernate 5 - Delete or Remove an Entity Example
- Hibernate 5 - load() Method Example
- Hibernate Session Interface Methods
- Hibernate Session.clear() Method Example
- Introduction Of Java strutes to Architecture
- Struts 2 - Architecture
- Struts 2 - Configuration Files
- Struts 2 - Actions
- Struts 2 - Interceptors
- Struts 2 - Results & Result Types
- Struts 2 - Value Stack/OGNL
- Struts 2 - File Uploading
- Struts 2 - Database Access
- Struts 2 - Validations Framework
JAVA FX
- JavaFX Tutorial
- Introduction to JavaFX Pane
- JavaFX Popup
- JavaFX group
- JavaFX Controller
- JavaFX Gradient Color
- JavaFXAnchorPane
- JavaFXTabPane
- JavaFX Scene
- JavaFX Stage
- JavaFXWebView
- JavaFX Timeline
- JavaFX Timer
- JavaFX Image
- JavaFX Background
- JavaFX dialog
- JavaFX Font
- JavaFXTextArea
- JavaFXObservableList
- JavaFX GUI
- JavaFX FXML
- JavaFXEventHandler
- JavaFXGradle
- JavafxScrollpane
- JavaFXAPI
JavaFXObservableList
In JavaFX, collections are defined by the package known as javafx.collections that contains different interfaces such as ObservableList, ListChangeListener, ObservableMap ,MapChangeListener, and classes such as FXCollections, ListChangeListener.Change, MapChangeListener.Change etc. In this article, we will be focusing on the list known as ObservableList that permits tracking the changes when occurred. ListChangeListener.Change is a class that denotes the change that has been made to an ObservableList. First, we will see how to declare an ObservableList in JavaFX.
Declaration
Below is the declaration of ObservableList in JavaFX.
public interface ObservableList< E> extends List< E>, Observable
Methods
Following are the different methods of ObservableList in JavaFX.
-
addListener
Syntax:
void addListener(ListChangeListener super E> li)
Description:
A listener li will be added to the observable list.
Parameter:
li, the listener that listens to the changes in the list.
-
removeListener
Syntax:
void removeListener(ListChangeListener super E> li)
Description:
A listener li will be removed from the observable list. If it is not available, nothing will happen.
Parameter:
li, the listener that has to be removed.
-
addAll
Syntax:
boolean addAll(E… el)
Description:
A suitable method for the var-arg addition of elements.
Parameter:
el, elements that have to be added.
Return Value:
true, which is mentioned by Collection.add(E)
-
setAll
Syntax:
boolean setAll (E… el)
Description:
A suitable method for var-arg addition of elements after clearing the ObservableList.
Parameter:
el, elements that have to be set.
Return Value:
true, which is mentioned by Collection.add(E)
-
setAll
Syntax:
boolean setAll (Collection extends E> c)
Description:
A suitable method for adding all the elements from the collection after clearing the ObservableList.
Parameter:
c, the collection that contains elements that have to be added to the list.
Return Value:
true, which is mentioned by Collection.add(E)
-
removeAll
Syntax:
boolean removeAll (E… el)
Description:
A suitable method for var-arg removal of elements.
Parameter:
el, elements that have to be removed.
-
retainAll
Syntax:
boolean retainAll (E… el)
Description:
A suitable method for var-arg retaining of elements.
Parameter:
el, elements that have to be retained.
Return Value:
true if the ObservableList is changed due to calling this method.
-
remove
Syntax:
void remove(int from, int to)
Description:
Similar to sublist(from, to).clear(). ObservableList used this method for the same.
Parameter:
from, which is the starting of the range that has to be removed, and to, which is the ending of the range that has to be removed,
-
filtered
Syntax:
default FilteredList< E> filtered(Predicate< E> pr)
Description:
A FilteredList wrapper is created for this list by using the mentioned predicate.
Parameter:
pr, a predicate that has to be used.
Return Value:
A new filtered list.
-
sorted
Syntax:
default SortedList< E> sorted( Comparator< E> c)
Description:
A SortedList wrapper is created for this list by using the mentioned comparator.
Parameter:
c, a comparator that has to be used. If natural order, then null.
Return Value:
A new sorted list.
-
sorted
Syntax:
default SortedList< E> sorted( )
Description:
A SortedList wrapper is created for this list by natural ordering.
Return Value:
A new sorted list.
Examples of JavaFXObservableList
Now, we will see a few examples of ObservableList in JavaFX.
Example #1
JavaFX program to demonstrate ObservableList that consists of integer type elements.
Code:
//sample program to demonstrate the working of ObservableList import java.util.List; import java.util.ArrayList; import javafx.collections.ObservableList; import javafx.collections.ListChangeListener; import javafx.collections.FXCollections; //main class public class ObservableListSample { //main method public static void main(String[] args) { //create a list of integer type List< Integer> li = new ArrayList< Integer>(); //create an observable list ObservableList< Integer>oli = FXCollections.observableList(li); //add listener method oli.addListener(new ListChangeListener() { @Override //onChanged method public void onChanged(ListChangeListener.Change c) { System.out.println("Hey, a change occured. . . "); } }); //add an item to the observable List oli.add(22); System.out.println("Size of the observable list is: " + oli.size() ); li.add(44); System.out.println("Size of the observable list is: " + oli.size()); oli.add(66); System.out.println("Size of the observable list is: " + oli.size()); } }
Example #2
JavaFX program to demonstrate ObservableList that consists of string type elements.
Code:
//sample program to demonstrate the working of ObservableList import java.util.List; import java.util.ArrayList; import javafx.collections.ObservableList; import javafx.collections.ListChangeListener; import javafx.collections.FXCollections; //main class public class ObservableListSample { //main method public static void main(String[] args) { //create a list of string type List< String> li = new ArrayList< String>();//create an observable list ObservableList< String>oli = FXCollections.observableList(li); //add listener method oli.addListener(new ListChangeListener() { @Override //onChanged method public void onChanged(ListChangeListener.Change c) { System.out.println("Hey, a change occured. . . "); //check while (c.next()) { //if anything was added System.out.println("Hey, anything added ? " + c.wasAdded()); System.out.println("Hey, anything removed ? " + c.wasRemoved()); } } }); //add an item to the observable List oli.add("This is item 1"); System.out.println("Size of the observable list is: " + oli.size() + oli.toString()); li.add("This is item 2" ); System.out.println("Size of the observable list is: " + oli.size() + oli.toString()); oli.add("This is item 3"); System.out.println("Size of the observable list is: " + oli.size() + oli.toString()); li.add("This is item 4"); System.out.println("Size of the observable list is: " + oli.size() + oli.toString()); //remove oli.remove(1); System.out.println("Size of the observable list is: " + oli.size()+oli.toString()); oli.sort(null); System.out.println("Size of the observable list is: " + oli.size()+oli.toString()); oli.set(2, "This is item 5"); System.out.println("Size of the observable list is: " + oli.size()+oli.toString()); } }
Apply now for Advanced Java Training Course