gpt4 book ai didi

java - 单击 JTable 中我的图标的操作

转载 作者:行者123 更新时间:2023-11-29 06:39:03 25 4
gpt4 key购买 nike

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

我想在单击 JTable 中的图标时添加一个 Action ,

图像渲染器

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 可以在每个事件、鼠标和按键事件上触发它的事件

enter image description here enter image description here enter 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();
}
});
}
}

关于java - 单击 JTable 中我的图标的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15211461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com