1

I am putting some icons in my jtable's cell but problem is it is changing it's background color to table's background color....i want it to change it to row's background color...and also when it is selected it does't show the selection...Here is my code how i am setting icons....

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

and here is the out put...... enter image description here

Edit1 here is my Image Render class ;and making setOpaque true makes my icons white..

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;
}
}
Haseeb Wali
  • 1,181
  • 3
  • 14
  • 34
  • 1
    for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, use `UIManager.getIcon("OptionPane.errorIcon");` instead – mKorbel Jan 04 '13 at 08:02
  • A little but more info about the `ImageRender` would be nice... – MadProgrammer Jan 04 '13 at 08:06
  • @MadProgrammer here is my image Render class....given in code – Haseeb Wali Jan 04 '13 at 08:35
  • @HaseebWali Just as an addition `DefaultTableCellRenderer` extends from `JLabel` itself, so you could simple call `super.getTableCellRendererComponent`, followed by `setIcon` and `return this`. The `DefaultTableCellRenderer` is optimised for fast, low resource rendering, so you should take direct advantage of as you can ;) – MadProgrammer Jan 04 '13 at 08:53

1 Answers1

2

I think you need to implement the custom cell renderer as discussed here and also described here. The renderer has access to information if the current cell is focused or selected, so you can adjust background, foreground or even content any way you want. Mind that components may be opaque (have they own background) or not (the parent background or other content is visible through it). This is controlled through setOpaque(boolean).

Community
  • 1
  • 1
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • Thanks a lot for your answer i have to give the row color to my label and set it's opacity to true to be shown normal.... – Haseeb Wali Jan 04 '13 at 08:42