Click action to my Icon in JTable

丶灬走出姿态 提交于 2019-12-13 02:31:46

问题


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


回答1:


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

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



回答2:


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.




回答3:


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

}


来源:https://stackoverflow.com/questions/15211461/click-action-to-my-icon-in-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!