1

我创建了类 ImageRenderer,它允许我在 JTable 图标 png 中显示,

当我单击 JTable 中的图标时,我想添加一个动作,

图像渲染器

import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;


class ImageRenderer extends DefaultTableCellRenderer {
      JLabel lbl = new JLabel();
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        lbl.setIcon((ImageIcon)value);
        return lbl;

      }
    }

ImageIcon icon = new ImageIcon("myicon.png");
tabla.setValueAt(icon, 0, 7);
jTable1.getColumn("Link").setCellRenderer(new ImageRenderer());

谢谢

4

3 回答 3

3

您必须在表格中添加一个鼠标侦听器,并让它使用rowAtPoint()and确定单击的单元格columnAtPoint()

渲染器绘制的组件仅用作“橡皮图章”。它们不存在于组件层次结构中,因此不响应鼠标输入。

于 2013-03-04T21:31:29.870 回答
3

最简单的编码是 的实现prepareRenderer,例如

注意之前将所有图形对象准备为局部变量,不要,永远不要从文件加载图形,不要从 JDBC 流,因为 XxxRenderer 可以在每个鼠标和键事件上触发其事件

在此处输入图像描述在此处输入图像描述在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.table.*;

public class TableIcon extends JFrame {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame ();
    private JTable table;
    private JLabel myLabel = new JLabel("waiting");
    private int pHeight = 40;
    private ImageIcon errorIcon = (ImageIcon) UIManager.getIcon("OptionPane.errorIcon");
    private ImageIcon infoIcon = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
    private ImageIcon warnIcon = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");
    private ImageIcon questIcon = (ImageIcon) UIManager.getIcon("OptionPane.questionIcon");

    public TableIcon() {
        String[] columnNames = {"Picture", "Description"};
        Object[][] data = {{errorIcon, "About"}, {errorIcon, "Add"}, {errorIcon, "Copy"}, {errorIcon, "Copy"}};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model) {
            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component comp = super.prepareRenderer(renderer, row, column);
                JLabel jc = (JLabel) comp;
                if (column == 0) {
                    if (isRowSelected(row) && isColumnSelected(column)) {
                        jc.setIcon(infoIcon);
                    } else if (isRowSelected(row) && !isColumnSelected(column)) {
                        jc.setIcon(warnIcon);
                    } else {
                        jc.setIcon(jc.getIcon());
                    }
                }
                return comp;
            }
        };
        table.setRowHeight(pHeight);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane, BorderLayout.CENTER);
        myLabel.setPreferredSize(new Dimension(200, pHeight));
        myLabel.setHorizontalAlignment(SwingConstants.CENTER);
        frame.add(myLabel, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TableIcon frame = new TableIcon();
            }
        }); 
    }
}
于 2013-03-05T00:01:48.390 回答
1

它有一个简单的方法:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;


public class ImageIconTable {

    private final int imageHeight = 200;//todo change to desire height
    private JTable table;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ImageIconTable().initGUI();
            }
        });
    }

    public void initGUI() {
        JFrame frame = new JFrame();

        ImageIcon icon1 = null;// new ImageIcon( "c://test1.gif"); //its not worked with gif
        ImageIcon icon2 = null;// new ImageIcon( "c://test2.jpg"); //it work with jpg

        try {
            BufferedImage bufferedImage1 = ImageIO.read(new File("c://test1.gif"));
            icon1 = new ImageIcon(bufferedImage1); //it work with all type images
            //todo scale imageHeight
            BufferedImage bufferedImage2 = ImageIO.read(new File("c://test2.jpg"));
            icon2 = new ImageIcon(bufferedImage2); //it work with all type images
        } catch (IOException e) {
            e.printStackTrace();
        }

       /*
       approach 1
       Object[] columnNames = {"name","picture"};
        Object[][] rowData = {{"me", icon1}
                        ,{"boss", icon2}};*/

        //approach 2
        Vector columnNames = new Vector();
        columnNames.add("name");
        columnNames.add("picture");

        Vector rowData = new Vector();
        Vector row1 = new Vector();
        Vector row2 = new Vector();
        row1.add("me");
        row1.add(icon1);
        row2.add("boss");
        row2.add(icon2);
        rowData.add(row1);
        rowData.add(row2);

        DefaultTableModel tableModel = new DefaultTableModel(rowData,columnNames) {
            @Override
            public Class getColumnClass(int column) {
                if (column == 1) return ImageIcon.class;
                return Object.class;
            }
        };

        table = new JTable(tableModel);
        table.setRowHeight(imageHeight);

        frame.add(new JScrollPane(table), BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

}
于 2013-11-15T21:02:25.897 回答