添加监听器,ArrayList的 [英] Add listener to ArrayList

查看:325
本文介绍了添加监听器,ArrayList的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的一些动态对象添加到它一个ArrayList,我有一个JButton。运行我的程序时,ArrayList是空的,将JButton设置为的setEnabled(假)。我想启用我的按钮,每当有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 没有任何形式的通知机制。

ArrayList doesn't have any sort of notification mechanism.

我建议你写你自己的列表实施委托给私人的ArrayList 其存储,但增加了侦听通知...或内找到Java本身类似的事情的能力。 <一href=\"http://download.oracle.com/javase/6/docs/api/javax/swing/DefaultListModel.html\"><$c$c>DefaultListModel可以的为你工作,虽然它没有实现列表本身。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆