Java:JTable 未正确显示图标

发布于 2024-10-28 04:29:17 字数 2309 浏览 0 评论 0原文

在我的应用程序中,我需要在 JTable 中显示文件系统文件。当我单击 JTree 节点(可以是任何系统文件夹)时,该文件夹的内容将显示在 JTable 中。

在 JTable 的第一列(其中显示文件或文件夹图标的名称)中,从系统图标中获取图标并显示。 一切都运转良好。但是,问题在于,当渲染器渲染图标时,第一个文件(JTable 的第一行)的图标在所有行中重复。我的意思是 JTable 的后续行中的图标不会改变。这里我的代码是渲染获取图标并且模型将其显示在 JTable 中

class KeyIconCellRenderer extends DefaultTableCellRenderer {
    public KeyIconCellRenderer(String ext) {
        File file = new File(ext);
        Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
        setIcon(icon);
    }
}

,这是我使用渲染显示的代码,

private class Selection implements TreeSelectionListener {
    public void valueChanged(TreeSelectionEvent e) {
        Object[] myData= new Object[6];
        TreePath path = e.getPath();
        FileUtil util= new FileUtil();
        FileMetaData metaData;

        Vector<FileMetaData> vList = new Vector<FileMetaData>();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)treeMainView.getLastSelectedPathComponent();

        FileInfo info =(FileInfo)node.getUserObject();
        File filePath= info.getFilepath();
        vList=util.getChildList(filePath);
        dtModel.getDataVector().removeAllElements();

        for(int i=0;i<vList.size(); i++){
            Vector v= new Vector();
            metaData=(FileMetaData)vList.get(i);
            v.add(metaData.getName());
            tblMainView.getColumnModel().getColumn(0).setCellRenderer(new KeyIconCellRenderer(metaData.getClientpath()));
            v.add(metaData.getClientpath());
            if(metaData.isDirectory()){
                v.add("");
            }else
            {
                v.add((FileHelper.getSizeString(metaData.getSize())));
            }
            if(metaData.isDirectory()){
                v.add("");
            }else
            {
                v.add(new Date(metaData.getTime()));
            }
            if(metaData.isDirectory()){
                v.add("Folder");
            }else
            {
                v.add("File");
            }
            v.add("Pending Upload");

            dtModel.insertRow(0, v);
        }
        tblMainView.repaint();
    }
}

如所附图像所示,只有第一个文件的图标在所有行中重复,

请帮助,这将是一个巨大的恩惠, 谢谢

Screen shot

In my application, I need to display file system files in a JTable. When I click on the JTree node (which is any system folder), the contents of that folder are shown in the JTable.

In the first column of the JTable (where the name of the file or folder icon is shown), the icon is fetched from the system icon and is displayed.
Every thing is working fine. However, the problem is that when the renderer renders icon, the icon of the first file (first row of JTable) is repeated in all rows. I mean the icon does not change in the subsequent rows of the JTable. Here my code is in which a render gets icon and the model displays it in the JTable

class KeyIconCellRenderer extends DefaultTableCellRenderer {
    public KeyIconCellRenderer(String ext) {
        File file = new File(ext);
        Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
        setIcon(icon);
    }
}

and here is code where I am using render to display

private class Selection implements TreeSelectionListener {
    public void valueChanged(TreeSelectionEvent e) {
        Object[] myData= new Object[6];
        TreePath path = e.getPath();
        FileUtil util= new FileUtil();
        FileMetaData metaData;

        Vector<FileMetaData> vList = new Vector<FileMetaData>();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)treeMainView.getLastSelectedPathComponent();

        FileInfo info =(FileInfo)node.getUserObject();
        File filePath= info.getFilepath();
        vList=util.getChildList(filePath);
        dtModel.getDataVector().removeAllElements();

        for(int i=0;i<vList.size(); i++){
            Vector v= new Vector();
            metaData=(FileMetaData)vList.get(i);
            v.add(metaData.getName());
            tblMainView.getColumnModel().getColumn(0).setCellRenderer(new KeyIconCellRenderer(metaData.getClientpath()));
            v.add(metaData.getClientpath());
            if(metaData.isDirectory()){
                v.add("");
            }else
            {
                v.add((FileHelper.getSizeString(metaData.getSize())));
            }
            if(metaData.isDirectory()){
                v.add("");
            }else
            {
                v.add(new Date(metaData.getTime()));
            }
            if(metaData.isDirectory()){
                v.add("Folder");
            }else
            {
                v.add("File");
            }
            v.add("Pending Upload");

            dtModel.insertRow(0, v);
        }
        tblMainView.repaint();
    }
}

as in the attached image, only the icon of the fist file is repeated in all rows,

Please help, it will be a huge favor,
Thanks

Screen shot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱桃奶球 2024-11-04 04:29:17

[您的表面错误是在循环中重置表列的渲染器,每次在渲染器的构造函数中对当前图标进行硬编码。因此,文件图标适用于所有内容。

基本问题是您似乎没有完全理解 渲染器:它用于显示单元格数据,因为单元格数据作为 getXXCellRendererComponent 中的参数传递。这就是查找要使用的图标的地方。方法是将 File 对象存储在表格单元格中,并在每次调用该方法时查询适当的图标。

干杯
珍妮特

[Your surface mistake is to reset the table column's renderer in the loop, each time hard-coding the current icon in the the renderer's constructor. Consequently, the file-icon is used for all.

The basic problem is that you don't seem to fully understand the concept of a renderer: it's there to display the cell data as it is delivered as a parameter in its getXXCellRendererComponent. So that's the place to look-up the icon to use. The way to go is to store the File object in the table cell and query the appropriate icon every time the method is called.

Cheers
Jeanette

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文