0

I'm having the following problem:

I have two Classes named ActiveDateList and MainFrame. The ActiveDateList (written as singleton) is extending Arraylist and holds the currently active "MyDate".

ActiveDateList:

public class ActiveDateList extends ArrayList<MyDate>{

    private static ActiveDateList instance; 

    private ActiveDateList(){}

    public static ActiveDateList getInstance(){
        if(instance == null){
            instance = new ActiveDateList();
        }
        return instance;
    }

...

    public void addSorted(MyDate md){
    ...
    }
}

The MainFrame Class is a JavaFX Application which needs to use an ObservableList of "ActiveDateList".

Code to do so:

    ActiveDateList activeDatelist = ActiveDateList.getInstance();
    ObservableList<MyDate> observableADList = FXCollections.observableList(list);

However after doing so, I lose access to every self written method I have in ActiveDateList, because things like "addSorted" can not be accessed through the ObservableList and every time I only call activeDateList.addSorted(..) observableADList does obviously not notify the listener.

I feel like I need a Class which implements ObservableList and instead of having to override all the methods it just uses the ones from ArrayList class and leaves me the possibility to write my own methods in it.

I am thankful for every idea.

Oussama Ben Ghorbel
  • 2,132
  • 4
  • 17
  • 34
C Su.
  • 3
  • 2
  • Consider adding a `public static void addSorted(MyDate md, List list)` method, and having your current non-static addSorted method delegate to it. – VGR Jun 16 '17 at 14:53
  • Your singletons is not thread safe. I recommend using `enum` for your singletons, as it comes bundled with features singletons require (thread-safe initialization, private constructor by default) - check out [Implementing Singleton with enum](https://stackoverflow.com/questions/26285520/implementing-singleton-with-an-enum-in-java). If you prefer to stick with `class`, you should implement [double-checked locking](https://en.m.wikipedia.org/wiki/Double-checked_locking) if the singletons will be used across multiple threads. This isn't a solution, just a suggestion. – Vince Jun 16 '17 at 14:56

1 Answers1

0

You should be able to extend the javafx.collections.ModifiableObservableListBase class instead of ArrayList. You can then use the beginChange and endChange methods to notify listeners. See: https://docs.oracle.com/javase/8/javafx/api/javafx/collections/ObservableListBase.html#beginChange--

Apokralipsa
  • 2,674
  • 17
  • 28
  • Thanks for your suggestion, it sounds like it makes a lot of sense. However i now have to implement the inherited abstract methods doAdd, doRemove, doSet, get and size and don't know what to write in them. Should i add the List now as a variable of the class instead of having the class extend ArrayList? – C Su. Jun 18 '17 at 14:13
  • Yes, the javadoc for `ModifiableObservableListBase` shows an example of an implementation where those methods delegate to an `ArrayList`. You may consider using a `CopyOnWriteArrayList` instead if you need the class to be thread safe. – Apokralipsa Jun 18 '17 at 17:52