• 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
  • 相关阅读:
    接口和抽像类
    java中FastJson的json类型转换
    可参考的源码
    @Autowired注解和静态属性
    IDEA中,将项目加入maven管理。
    关于.idea未上传导致不能显示项目文件结构的问题
    Master-Worker模式
    Future模式
    了解Queue
    ConcurrentMap与CopyOnWrite容器
  • 原文地址:https://www.cnblogs.com/borter/p/9596135.html
Copyright © 2020-2023  润新知