简体   繁体   English

如何将图像添加到JTable单元格?

[英]How to add image to JTable cell?

I have a JTable that was created in Netbeans's design mode. 我有一个在Netbeans的设计模式下创建的JTable。 In my code, I opted for placing the following line of code on startup: 在我的代码中,我选择在启动时放置以下代码行:

model = new TableModel();
tbShares.setAutoCreateColumnsFromModel(false);
tbShares.setModel(model);

tbShares is my JTable object. tbShares是我的JTable对象。

The JFrame that contains my JTable is shown below: 包含我的JTable的JFrame如下所示:

股份经理

The first column was set as an Object type (the other two are Strings), so it can show an image. 第一列设置为Object类型(其他两列为字符串),因此它可以显示图像。 I have this code that I typed up for it to load an image, but it's not working (the list.add part, the rest is to give you an insight on how my code is structured). 我输入了此代码以加载图像,但是它不起作用( list.add部分,其余部分是让您了解我的代码的结构)。 This is the extension of an AbstractTableModel . 这是AbstractTableModel的扩展。

public void addRegister(String status, String name, String clients){
        ImageIcon activeStatus = new ImageIcon(CleanSheets.class.getResource("res/img/active.png"));
        ImageIcon inactiveStatus = new ImageIcon(CleanSheets.class.getResource("res/img/inactive.png"));
        list.add(new Register((status.equals("true") ? activeStatus : inactiveStatus), name, clients));
        this.fireTableDataChanged();
    }

    class Register{
        Object status;
        String name;
        String clients;

        public Register(Object status, String name, String clients) {
            this.status = status;
            this.name = name;
            this.clients = clients;
        }
    }

Supposedly, it grabs the images from the folder I indicated, but then it's just outputting text in that column instead of the actual image. 据说,它从我指示的文件夹中抓取图像,但是它只是在该列中输出文本,而不是实际图像。 How do I get it to show the correct image? 如何获得显示正确图像的信息? Thank you. 谢谢。

您应该重新实现TableCellRenderer尝试阅读简短文章

ImageIcon activeStatus = new ImageIcon(CleanSheets.class.getResource("res/img/active.png")); ImageIcon activeStatus = new ImageIcon(CleanSheets.class.getResource(“ res / img / active.png”));; ImageIcon inactiveStatus = new ImageIcon(CleanSheets.class.getResource("res/img/inactive.png")); ImageIcon inactiveStatus =新的ImageIcon(CleanSheets.class.getResource(“ res / img / inactive.png”));;

  • prepare Icon/ImageIcon as local variable, because Renderer recreating these Objects on each of Mouse or Key events , there are a bunch events in crazy period 准备Icon/ImageIcon作为局部变量,因为Renderer在每个Mouse or Key events上都会重新创建这些Objects ,因此在疯狂时期会有很多事件

this.fireTableDataChanged(); this.fireTableDataChanged();

  • is correct notifier for add / remove whole JTable contens, for TableCell there are fireTableCellXxx() 是添加/删除整个JTable内容的正确通知者,对于TableCell,有fireTableCellXxx()

  • in this context is (add only one row to the TableModel ) about fireTableRowsInserted() 在这种情况下,(仅向TableModel添加一行)关于fireTableRowsInserted()

How do I get it to show the correct image? 如何获得显示正确图像的信息?

  • as I see there you add a new row with, then add Icon / ImageIcon to the TableModel directly, no more conversion, declare required, no issue, JTable has implemented Icon / ImageIcon in the API 如我所见,您在其中添加了新行,然后将Icon / ImageIcon直接添加到TableModel ,不再进行转换,无需声明,没有问题,JTable已在API中实现了Icon / ImageIcon

  • don't use Renderer if Icon / ImageIcon aren't changed form Mouse or Key events 如果没有从Mouse or Key events更改Icon / ImageIcon请不要使用Renderer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM