将图像插入JTable [英] Inserting an Image into a JTable

查看:77
本文介绍了将图像插入JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将图像插入JTable时遇到问题.香港专业教育学院搜索,发现有两种方法.您可以覆盖tablemodel的getcolumn类,也可以创建自定义tablecellrender.

I'm having an issue inserting images into a JTable. Ive searched and found there are 2 ways. You can either override the getcolumnclass of the tablemodel or create your custom tablecellrender.

我选择使用以下代码制作自己的单元格渲染器:

I chose to make my own cell renderer with this code:

public class MyRenderer extends DefaultTableCellRenderer {

    /*
     * @see TableCellRenderer#getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int)
     */
    public Component getTableCellRendererComponent(JTable table, Object value,
                                                 boolean isSelected, boolean hasFocus, 
                                                 int row, int column) {
      ImageIcon icon = new ImageIcon("Images/green.png");
      setText((String)value);
      setIcon(icon);
      return this;
    }
}

在我填写表格字段的方法中,我用这一行添加图像.

In the method where I fill in my table's fields, I am adding the images with this line..

laneTable.getColumnModel().getColumn(4).setCellRenderer(new MyRenderer());

问题在于图像显示为空白区域.它的确可以渲染某些内容,因为当您选择该行时,所有字段均以蓝色突出显示,但第4列(带有图像的那一列)仍为白色.我一辈子都无法弄清楚为什么没有显示图像.我已经通过各种方式调整了行高,以查看图像是否在那里,但是看不见东西.似乎并非如此.

The problem is that the images are showing up as blank white space. Its definitely rendering something because when you select the row, all of the fields highlight blue except for the 4th column(the one with the images) is still white. I cannot for the life of me figure out why the images are not showing. I've adjusted the row height in all sorts of ways to see if maybe the image was there, but out of view or something. Doesn't seem to be the case.

我也尝试做替代方法,但是我猜我没有正确地做语法,因为netbeans给出了语法错误,而且我还没有找到一个很好的例子.

I also tried to do the override method but I guess I didn't do the syntax correctly because netbeans was giving syntax errors, and I have yet to find a decent example on how to do it.

谢谢.

edit:我尝试了一种更简单的方法,使用下面的代码覆盖该类,但是单元格仍然为空.而且我已经测试了将图像插入标签并可以正常工作,因此路径是正确的.

edit: I've attempted the easier way, overriding the class using the code below but the cells are still blank. And I have tested inserting images into labels and those work, so the path is correct.

           @Override
        public Class getColumnClass(int column)
        {

            if (column == 4)
            {

                return ImageIcon.class;
            }
            return Object.class;
            // other code; default to Object.class
        }

推荐答案

我选择使用此代码制作自己的单元格渲染器.

I chose to make my own cell renderer with this code..

为什么要重新发明轮子?如您所见,您遇到了问题,因此只需使用默认渲染器并覆盖getColumnClass()方法即可.

Why reinvent the wheel? As you can see you are having problems, so just use the default renderer and override the getColumnClass() method.

此外,您永远都不会读取代码中的图像.

Also, you would NEVER read the image in the rednering code.

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableIcon extends JFrame
{
    public TableIcon()
    {
        ImageIcon aboutIcon = new ImageIcon("about16.gif");
        ImageIcon addIcon = new ImageIcon("add16.gif");
        ImageIcon copyIcon = new ImageIcon("copy16.gif");

        String[] columnNames = {"Picture", "Description"};
        Object[][] data =
        {
            {aboutIcon, "About"},
            {addIcon, "Add"},
            {copyIcon, "Copy"},
        };

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable( model )
        {
            //  Returning the Class of each column will allow different
            //  renderers to be used based on Class
            public Class getColumnClass(int column)
            {
                return getValueAt(0, column).getClass();
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        TableIcon frame = new TableIcon();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }

}

更新:

也许这更容易理解:

public Class getColumnClass(int column)
{
//  return getValueAt(0, column).getClass();
    return (column == 0) ? Icon.class : Object.class;
}

这篇关于将图像插入JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆