I have an ArrayList which I add some Objects to it dynamically, and I have a JButton. The ArrayList is empty when running my program and the JButton is set to setEnabled(fal
You can't do such thing with ArrayList because as @Jon Skeet says ArrayList doesn't have any sort of notification mechanism. You should try JGoodies binding ObservableList
that could help.
Or you could set up a timer that will check for the size of ArrayList and change JButton accordingly. You will require a thread to do this job of monitoring list at some time interval.
Or if you know all the place where you add/remove elements from list then write this login at all that place.
ArrayList
doesn't have any sort of notification mechanism.
I suggest you write your own List
implementation which delegates to a private ArrayList
for its storage, but adds the ability to listen for notifications... or find something similar within Java itself. DefaultListModel may work for you, although it doesn't implement List
itself.
If you need to fix this issue using only java.util.ArrayList you can use below solution. I don't know this is the exact solution you want. But you can achieve your need as below.
Implement what should happen after added and removed. Customize this interface as you needed.
public interface ListBehaviorHandler {
void afterAdded();
void afterDeleted();
}
And use below method to get custom behave list.
public <E> List<E> getCustomList(ListBehaviorHandler listBehaviorHandler) {
return new ArrayList<E>() {
@Override
public boolean add(E e) {
boolean added = super.add(e);
listBehaviorHandler.afterAdded();
return added;
}
@Override
public void add(int index, E element) {
super.add(index, element);
listBehaviorHandler.afterAdded();
}
@Override
public E remove(int index) {
E removed = super.remove(index);
listBehaviorHandler.afterDeleted();
return removed;
}
@Override
public boolean remove(Object o) {
boolean removed = super.remove(o);
listBehaviorHandler.afterDeleted();
return removed;
}
};
}
Javafx (part of JRE 8) provides an observable list implementation. This code works for me:
ObservableList<MyAnno> lstAnno1 = FXCollections.observableList(new ArrayList<MyAnno>());
lstAnno1.addListener((ListChangeListener.Change<? extends MyAnno> c) -> {
c.next();
updateAnnotation((List<MyAnno>) c.getAddedSubList(), xyPlot);
});
...
lstAnno1.add(new MyAnno(lTs, dValue, strType));
...
public void updateAnnotation(List<MyAnno> lstMyAnno, XYPlot xyPlot) {
lstMyAnno.forEach(d -> {
...
});
}
As @Jon Skeet suggests you can also do something like :
public class ListResponseModel<E> extends AbstractListModel {
private static final long serialVersionUID = 1L;
private ArrayList<E> delegate = new ArrayList<E>();
@Override
public int getSize() {
return delegate.size();
}
@Override
public Object getElementAt(int index) {
return delegate.get(index);
}
public void add(E e){
int index = delegate.size();
delegate.add(e);
fireIntervalAdded(this, index, index);
}
}
If you write your own List
implementation, as @Jon Skeet suggests, you can give it an EventListenerList. The API outlines the relevant methods.