ToMakPo
ToMakPo

Reputation: 864

Java: Listen if a value in a List changes

I need to know how to create a listener that watches for changes in a List object and then does something, I've been trying to google it, but I don't really know what to look for.

I have a class called Pencil. Pencils are always automatically added to a List that gets passed to it when created. There are different lists depending on who is creating the pencil. Index 0 for all of the lists is the currently selected pencil. For example:

index 0: Pencil-2445
index 1: Pencil-7582
index 2: Pencil-1334
index 3: Pencil-2445 //<-- Note that this Pencil is also in index 0.
index 4: Pencil-4824
index 5: Pencil-3482

When a pencil is selected, the index 0 is automatically updated on the Pencil class. The class that created the pencil can see the list of pencils. Depending on the class, what happens when the pencil is selected will be different from one class to the next. When a pencil is selected and the list[0] is updated, I need the class to see that index 0 was updated and to do whatever it is that needs to be done.

I know you guys like to see code examples, but there is so much code I could include and most of it is probably irrelevant, but I don't know what you need.

Upvotes: 0

Views: 454

Answers (1)

Remon Shehatta
Remon Shehatta

Reputation: 1470

I think what you are looking for is called "Observer design pattern". it's best that you take a look at this tutorial and try to understand how things work by yourself.

if you need further assistance or more explanation let me know.

Upvotes: 2

Related Questions