gpt4 book ai didi

java - 停止重复图标

转载 作者:行者123 更新时间:2023-12-01 10:06:31 25 4
gpt4 key购买 nike

我正在制作一个 JTable,它可以覆盖将图标放入所需的表中。这就是我所做的。但结果在所有单元格中打印图标,而不仅仅是我的第一个单元格,这不是我想要的。结果如下:

enter image description here

import com.skype.User;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

class BoardTableCellRenderer extends DefaultTableCellRenderer {


public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int col) {

Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, col);
JLabel label = (JLabel)super.getTableCellRendererComponent(table, value,isSelected, hasFocus,row, col);
//row is the username
if(col == 0) {
try {
User temp = Functions.returnUser(value.toString());
ImageIcon imageIcon = new ImageIcon(getClass().getResource("/" + temp.getStatus().toString() + ".png"));
label.setIcon(imageIcon);
} catch (Exception ex) {
}
return label;
}else{
return c;
}

}
}

最佳答案

label.setIcon(null); 添加到您的 else block ,否则您只是返回上次设置的内容

参见Concepts: Editors and Renderers了解更多详情

关于java - 停止重复图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36416222/

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