• Post Reply Bookmark Topic Watch Topic
  • New Topic

Controller and Sorted List, sorting.  RSS feed

 
John Doherty
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

I am coding along to my course and i came across this sorted list that sorts our program every time we make a adjustment to it. I have used IntelliJ to write it. I understand how it sorts out itself. I don't know how it gets executed every time we add a new item to the list or we erase something from the list. Sorted List code block is like below.



My listener who listens to changes is below too.


 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is obviously some overriding in the add() method on the sorted list class, which causes the new element to be added in the appropriate location to maintain the sorted order. I cannot tell how that is implemented, but you may be able to find the code of the sorted list class and read it.
 
John Doherty
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is obviously some overriding in the add() method on the sorted list class, which causes the new element to be added in the appropriate location to maintain the sorted order. I cannot tell how that is implemented, but you may be able to find the code of the sorted list class and read it.


I will look into that but it sorts the list after every action deleting too.  Deleting item is below:

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code doesn't tell us how the List deletes an element; it tells us how your code calls the remove method. Or more precisely, part of how that method is called.
Deleting an element from a sorted collection doesn't alter the order of the remainder of the collection, so removal is easy. It is insertion that is the difficult part. Is this the class you are using. You didn't say it was a standard FX class; I thought it was one you had written yourself.
 
John Doherty
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I open a new dialog to add the new item which has a code like below:


which leads to

 
John Doherty
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i found what i am after. So sorted list makes changes to itself every time its observable list has a change. It says so below:

https://docs.oracle.com/javase/8/javafx/api/javafx/collections/transformation/SortedList.html

Can you explain to me how its triggered ? What works in the background to trigger this change ?

PS:I might have gotten this wrong so if i did excuse me.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know, but I believe Observable List implements the Observable interface, so it can signal changes to its Observer, which in this case is the sorted list wrapper. Search for observer pattern.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yesterday, I wrote:. . . Search for observer pattern.
And today I found this FAQ link. That may be a bit old for JavaFX.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!