代码之家  ›  专栏  ›  技术社区  ›  Silver moon

使用Swing显示文件状态的好方法是什么

  •  -1
  • Silver moon  · 技术社区  · 6 年前

    我想做以下几点,我需要一些关于什么是最好的方法做它的建议。

    我正在考虑将一个ImageIcon与每个添加的文件相关联,但我不知道如何更改其外观以显示状态。我也不知道如何图像图标可以删除时,文件被删除。有没有其他方法(除了ImageIcon)可以做到这一点?如有任何帮助/建议,我们将不胜感激。

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   MadProgrammer    6 年前

    在编程中,数据为王。数据的表示方式不应该考虑到数据,这是UI/视图层的域/职责。

    这通常由 model-view-controller pattern

    在您的示例中,有两条(基本)信息。一个文件和一个状态(未运行、运行、已删除),您要将此信息组合为“数据”。在Java中,这通常意味着一个普通的旧Java对象(或Pojo)

    因为状态只有有限的可能性,我们可以使用 enum 来表示它,从而限制有效值

    public enum FileStatus {
        NOT_RUN, RUN, DELETED;
    }
    

    然后我们可以创建自己的pojo。。。

    public class FileOperation {
        private File file;
        private FileStatus status;
    
        public FileOperation(File file, FileStatus status) {
            this.file = file;
            this.status = status;
        }
    
        public FileOperation(File file) {
            this(file, FileStatus.NOT_RUN);
        }
    
        public File getFile() {
            return file;
        }
    
        public FileStatus getStatus() {
            return status;
        }
    
        public void setStatus(FileStatus newStatus) {
            if (status == newStatus) {
                return;
            }
            this.status = newStatus;
        }
    
    }
    

    现在,当我们想知道文件的状态时,我们知道从哪里获取它。

    JList ? 你问,好问题。我们真正想要的是 当任何 FileOperation

    现在,你可以迭代 ListModel ,但这不是一个非常干净的解决方案,更好的解决方案是允许 文件操作 当事件发生变化时生成事件并 列表模型 倾听他们的声音,并采取自己的行动。

    observer pattern

    public class FileOperation {
        private File file;
        private FileStatus status;
    
        private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
    
        public FileOperation(File file, FileStatus status) {
            this.file = file;
            this.status = status;
        }
    
        public FileOperation(File file) {
            this(file, FileStatus.NOT_RUN);
        }
    
        public File getFile() {
            return file;
        }
    
        public FileStatus getStatus() {
            return status;
        }
    
        public void setStatus(FileStatus newStatus) {
            if (status == newStatus) {
                return;
            }
            FileStatus oldStatus = status;
            status = newStatus;
            propertyChangeSupport.firePropertyChange("status", oldStatus, status);
        }
    
        public void addPropertyChangeListener(PropertyChangeListener listener) {
            propertyChangeSupport.addPropertyChangeListener(listener);
        }
    
        public void removePropertyChangeListener(PropertyChangeListener listener) {
            propertyChangeSupport.removePropertyChangeListener(listener);
        }
    }
    

    现在我们需要一个 它能回应。。。

    public class FileOperationListModel extends AbstractListModel<FileOperation> {
    
        private List<FileOperation> items = new ArrayList<FileOperation>(25);
        private PropertyChangeListener handler = new PropertyChangeHandler();
    
        public void add(FileOperation fo) {
            fo.addPropertyChangeListener(handler);
            int size = items.size();
            items.add(fo);
            fireIntervalAdded(this, size, size);
        }
    
        public void remove(FileOperation fo) {
            int index = items.indexOf(fo);
            if (index < 0) {
                return;
            }
            fo.removePropertyChangeListener(handler);
            items.remove(fo);
            fireIntervalRemoved(this, index, index);
        }
    
        @Override
        public int getSize() {
            return items.size();
        }
    
        @Override
        public FileOperation getElementAt(int index) {
            return items.get(index);
        }
    
        public class PropertyChangeHandler implements PropertyChangeListener {
    
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (!(evt.getSource() instanceof FileOperation)) {
                    return;
                }
                FileOperation fo = (FileOperation) evt.getSource();
                int index = items.indexOf(fo);
                fireContentsChanged(FileOperationListModel.this, index, index);
            }
    
        }
    
    }
    

    ListCellRenderer 它可以显示您想要的信息。

    为此,你必须从阅读开始 How to use lists Writing a Custom Cell Renderer