Click action to my Icon in JTable

2019-08-09 01:21发布

I create class ImageRenderer that which allows me to display in JTable icon png,

i would like to add a action when i click on the icon in JTable,

ImageRenderer

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;

      }
    }

and

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

thanks

3条回答
Ridiculous、
2楼-- · 2019-08-09 01:45

it has a simple way:

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);
    }

}
查看更多
Evening l夕情丶
3楼-- · 2019-08-09 01:49

You will have to add a mouse listener to the table, and have it determine the clicked cell using rowAtPoint() and columnAtPoint().

The components drawn by a renderer are just used as a "rubber stamp". They do not exist in the component hierarchy and so do not respond to mouse input.

查看更多
太酷不给撩
4楼-- · 2019-08-09 01:59

and easiest for coding is implementations of prepareRenderer, for example

notice prepare all Graphics Objects before as local variable, don't, never load Graphics from File, Stream not from JDBC, becuse XxxRenderer can fired its events on every, mouse, and keys events

enter image description hereenter image description hereenter image description here

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();
            }
        }); 
    }
}
查看更多
登录 后发表回答