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(false). I want to enable my button whenever there are 2 elements in the ArrayList or more and disable it again if the ArrayList has one item or empty. How can I achieve this?
Share
ArrayListdoesn’t have any sort of notification mechanism.I suggest you write your own
Listimplementation which delegates to a privateArrayListfor its storage, but adds the ability to listen for notifications… or find something similar within Java itself.DefaultListModelmay work for you, although it doesn’t implementListitself.