Add listener to ArrayList

前端 未结 8 1308
再見小時候
再見小時候 2020-12-09 17:14

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

相关标签:
8条回答
  • 2020-12-09 17:28

    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.

    0 讨论(0)
  • 2020-12-09 17:33

    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.

    0 讨论(0)
  • 2020-12-09 17:34

    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;
            }
        };
    }
    
    0 讨论(0)
  • 2020-12-09 17:37

    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 -> {
       ...
       });
    }
    
    0 讨论(0)
  • 2020-12-09 17:37

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-09 17:40

    If you write your own List implementation, as @Jon Skeet suggests, you can give it an EventListenerList. The API outlines the relevant methods.

    0 讨论(0)
提交回复
热议问题