如何在JTable列中设置图标? [英] How to set icon in a column of JTable?

查看:161
本文介绍了如何在JTable列中设置图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以设置列的标题,但无法在JTable的第一列的所有行中设置图标。

I am able to set the column's header but not able to set icon in all the rows of first column of JTable.

public class iconRenderer extends DefaultTableCellRenderer{
    public Component getTableCellRendererComponent(JTable table,Object obj,boolean isSelected,boolean hasFocus,int row,int column){
        imageicon i=(imageicon)obj;
        if(obj==i)
            setIcon(i.imageIcon);
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));
        setHorizontalAlignment(JLabel.CENTER);
        return this;
    }
}

public class imageicon{
    ImageIcon imageIcon;
    imageicon(ImageIcon icon){
        imageIcon=icon;
    }
}  






我的BuildTable()方法中的以下行。


and below lines in my BuildTable() method.

    public void SetIcon(JTable table, int col_index, ImageIcon icon){
      table.getTableHeader().getColumnModel().getColumn(col_index).setHeaderRenderer(new iconRenderer());
      table.getColumnModel().getColumn(col_index).setHeaderValue(new imageicon(icon));
}






我们如何设置它对于第一列的所有行?我已尝试使用for循环,但还没有得到行迭代来设置图标。或者还有其他方法吗?


How can we set it for all rows of first columns? I have tried with for loop but didnt get yet for rows to iterate to set icon. Or is there any other way?

推荐答案

无需创建自定义渲染。 JTable已经支持Icon渲染器。你只需告诉表使用这个渲染器。这是通过覆盖表模型的getColumnClass(...)方法完成的:

There is no need to create a custom render. JTable already supports an Icon renderer. YOu just need to tell the table to use this renderer. This is done by overriding the getColumnClass(...) method of the table model:

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

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

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

        DefaultTableModel model = new DefaultTableModel(data, columnNames)
        {
            //  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();
            }
        };
        JTable table = new JTable( model );
        table.setPreferredScrollableViewportSize(table.getPreferredSize());

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

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

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

}

这篇关于如何在JTable列中设置图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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