e785. 监听JList中项的变动

Posted borter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了e785. 监听JList中项的变动相关的知识,希望对你有一定的参考价值。

When the set of items in a list component is changed, a list data event is fired.

    // Create a list that allows adds and removes;
    // see e778 在JList中加入和删除项
    
    // Register a list data listener
    DefaultListModel model = (DefaultListModel)list.getModel();
    model.addListDataListener(new MyListDataListener());
    
    class MyListDataListener implements ListDataListener {
        // This method is called when new items have been added to the list
        public void intervalAdded(ListDataEvent evt) {
            DefaultListModel model = (DefaultListModel)evt.getSource();
    
            // Get range of new  items
            int start = evt.getIndex0();
            int end = evt.getIndex1();
            int count = end-start+1;
    
            // Get new items
            for (int i=start; i<=end; i++) {
                Object item = model.getElementAt(i);
            }
        }
    
        // This method is called when items have been removed from the list
        public void intervalRemoved(ListDataEvent evt) {
            // Get range of removed items
            int start = evt.getIndex0();
            int end = evt.getIndex1();
            int count = end-start+1;
    
            // The removed items are not available
        }
    
    
        // This method is called when items in the list are replaced
        public void contentsChanged(ListDataEvent evt) {
            DefaultListModel model = (DefaultListModel)evt.getSource();
    
            // Get range of changed items
            int start = evt.getIndex0();
            int end = evt.getIndex1();
            int count = end-start+1;
    
            // Get changed items
            for (int i=start; i<=end; i++) {
                Object item = model.getElementAt(i);
            }
        }
    }

 

Related Examples

以上是关于e785. 监听JList中项的变动的主要内容,如果未能解决你的问题,请参考以下文章

监听JList列表项的单击事件

如何绑定listView中项的子属性?

检索 JList 项的数组数据

计算另一个表中每个事务中项的数量

26)PHP,数据库表格中项的数据类型

2021-11-24 WinFrom面试题 如何根据提供的值列表,设置CheckedListBox中项的勾选状态?