我將一些圖標放在我的jtable的單元格中,但問題是它將它的背景顏色更改爲表格的背景顏色....我希望它將其更改爲行的背景顏色.. 。而也被選中時,它簡化版,顯示選擇......這裏是我的代碼是如何設置的圖標....把圖標放在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;
}
}
更快速地發佈[SSCCE](http://sscce.org/),簡短,可運行,可編譯,使用'UIManager.getIcon(「OptionPane.errorIcon」);'代替 – mKorbel
稍微更多信息關於'ImageRender'會很好... – MadProgrammer
@MadProgrammer這裏是我的圖像渲染類....代碼 –