我在我的 jtable 的单元格中放置了一些图标,但问题是它正在将其背景颜色更改为表格的背景颜色....我希望它将其更改为行的背景颜色...并且当它被选中时它不显示选择...这是我如何设置图标的代码...
Users user;
ConsoleUsersListTbl.getColumnModel().getColumn(1).setCellRenderer(new ImageRender());
DefaultTableModel userTableModel = (DefaultTableModel) ConsoleUsersListTbl.getModel();
for (int i = 0; i < userList.size()-1; i++) {
user = userList.get(i);
javax.swing.ImageIcon image_icon = new javax.swing.ImageIcon(user.getUser_image());
if (image_icon.getIconWidth() > 32 || image_icon.getIconWidth() > 32) {
InputStream in = new ByteArrayInputStream(user.getUser_image());
BufferedImage buff_image;
try {
buff_image = ImageIO.read(in);
int type = buff_image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : buff_image.getType();
BufferedImage resizedImage = resizeImage(buff_image, type);
image_icon.setImage(resizedImage);
userTableModel.setValueAt(image_icon, i, 1);
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
userTableModel.setValueAt(image_icon, i, 1);
}
这是输出......
Edit1 这里是我的图像渲染类;使 setOpaque 为真使我的图标变白..
public class ImageRender extends DefaultTableCellRenderer {
JLabel lable = new JLabel();
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
lable.setIcon((ImageIcon)value);
lable.setOpaque(true);
return lable;
}
}