本文介绍了在JTable中对我的图标单击操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

当我单击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;

      }
    }

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

谢谢

解决方案

最容易编码的是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();
            }
        });
    }
}

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

解决方案

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

这篇关于在JTable中对我的图标单击操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 13:32