将侦听器添加到 ArrayList [英] Add listener to ArrayList
问题描述
我有一个 ArrayList,我动态地向它添加了一些对象,我有一个 JButton.运行我的程序时 ArrayList 为空,并且 JButton 设置为 setEnabled(false).我想在 ArrayList 中有 2 个或更多元素时启用我的按钮,如果 ArrayList 有一个项目或为空,则再次禁用它.我怎样才能做到这一点?
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?
推荐答案
ArrayList
没有任何通知机制.
我建议您编写自己的 List
实现,该实现委托给私有 ArrayList
进行存储,但增加了侦听通知的能力...或找到类似的东西在 Java 本身中.DefaultListModel
可能对你有用,虽然它没有实现 List
本身.
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.
这篇关于将侦听器添加到 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!