如何将图像插入Jtable(自定义代码) [英] How to Insert Image into Jtable(Customize code)

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

问题描述

当我尝试在NetTables生成的JTable中插入时,遇到了很多困难.

这是我的JTable的自定义代码的捕获.

I got many difficulties when I tried to insert in JTable which netbeans generated.

this capture of the customize code of my JTable.

所以我如何在班级代码中准确地将Image设置为jtable.

so how I set Image into jtable in my class code accurately.

推荐答案

您需要覆盖getColumnClass()方法,以便表可以选择适当的渲染器.简单的例子:

You need to override the getColumnClass() method so the table can choose an appropriate renderer. Simple example:

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 (column == 0) ? Icon.class : Object.class;
            }
        };
        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);
    }

}

这篇关于如何将图像插入Jtable(自定义代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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