简体   繁体   English

将图像图标设置为特定的单元格jTable

[英]set Image Icon to particular cell jTable

I am newbee in Jtable swing ,I want to set the image icon to particular cell of Jtable, 我是Jtable swing的newbee,我想将图像图标设置为Jtable的特定单元格,

So I tried following code. 所以我尝试了以下代码。

ImageIcon addIcon = new ImageIcon("addIcon.gif"); //addIcon.gif is stored in the resource packaage
table.setModel(new javax.swing.table.DefaultTableModel
    (
        new Object [][]
        {
            { 
                rowNumber, null, null, null,addIcon
            },
            {
                null,null ,"Total" ,"0.0","Get Total"
            }
        },
        new String [] 
        {
            "No.", "Item", "Weight", "Amount","#"
        }
    )
        {
            @Override
            public Class<?> getColumnClass(int c) 
            {
                return getValueAt(0, c).getClass();
            }
        }
    );

But instead of Icon, I am getting the "addIcon.gif" string in that jTable cell. 但是,我在该jTable单元中获得的是“ addIcon.gif”字符串,而不是Icon。 What is the mistake I did here. 我在这里犯了什么错误。

Please help. 请帮忙。

You need to implement your own TableCellRenderer: https://docs.oracle.com/javase/8/docs/api/javax/swing/table/TableCellRenderer.html . 您需要实现自己的TableCellRenderer: https : //docs.oracle.com/javase/8/docs/api/javax/swing/table/TableCellRenderer.html The table model only holds the icon, it does no drawing. 表格模型仅包含图标,不绘制图形。 Drawing is done by cell renderers (to separate UI and data). 绘制由单元格渲染器完成(以分隔UI和数据)。 See also the JTable tutorial about custom cell renderers: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer 另请参见有关自定义单元格渲染器的JTable教程: http : //docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer

You can't just override the getColumnClass(...) method because that class will be used to determine the renderer for all rows for a given column in the table. 您不能只覆盖getColumnClass(...)方法,因为该类将用于确定表中给定列的所有行的呈现器。

For specific cell rendering you can override the getCellRenderer(...) method fo the table: 对于特定的单元格渲染,您可以覆盖表的getCellRenderer(...)方法:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.table.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        ImageIcon addIcon = new ImageIcon("copy16.gif");

        String[] columnNames = {"No.", "Item", "Weight", "Amount","#"};

        Object[][] data =
        {
            {"123", null, null, null, addIcon},
            {null, null ,"Total" ,"0.0", "Get Total"}
        };

        DefaultTableModel model = new DefaultTableModel(data, columnNames);

        JTable table = new JTable(model)
        {
            @Override
            public TableCellRenderer getCellRenderer(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel(column);

                if (modelColumn == 4 && row == 0)
                {
                    return getDefaultRenderer( Icon.class );
                }
                else
                    return super.getCellRenderer(row, column);
            }
        };

        add( new JScrollPane( table ) );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SSCCE() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

This is an example of a SSCCE . 这是SSCCE的示例。 A SSCCE should be included with all questions to demonstrate your problem. 所有问题都应包含SSCCE,以证明您的问题。

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

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