Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Issues setting background color of a Jlabel in a Jtable, once ImageIcon

807600Jun 11 2007 — edited Jun 14 2007
I am having issues setting the background color of a Jlabel in a Jtable, once ImageIcon is set. The problem is that when a row is selected, every cell but the one with an image icon reflects the selection color, while the cell with the ImageIcon shows the defailt background color, regardless of selection.

Here is the code for my TableCellRenderer which is not working :
        TableCellRenderer tcr = new TableCellRenderer() {
            public Component getTableCellRendererComponent(JTable table,
                    Object value,
                    boolean isSelected,
                    boolean hasFocus,
                    int row,
                    int col){
                JLabel jl = new JLabel();
                try {
                    if (value.toString().substring(0,1).equals("*")){
                        String fl = "/Users/XXX/XXX/FEEDICONS/"+rs.getInt("feed_id")+".png";
                        jl.setText(value.toString().substring(1));
                        jl.setIcon(new ImageIcon(fl));
                        if (isSelected) jl.setBackground(new Color(180,213,255));
                         return (Component) jl;
                    } else {
                        TableCellRenderer dtcr;
                        dtcr = table.getDefaultRenderer(Object.class);
                        return dtcr.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col);
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                return null;
            }
        };
I am fairly new to Java/Swing so, most likely I'm missing something obvious.

Any help is appreciated. Thanks!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 12 2007
Added on Jun 11 2007
6 comments
634 views