Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2008

Tip: Looking for answers? Try searching our database.

TableCellRender puzzle

Thread view: 
Roedy Green - 12 Jan 2008 13:18 GMT
Below is a fairly simple TableCellRenderer for displaying an enum as
an Icon.  That works fine. But I added a wrinkle, changing the
background colour depending on whether the row is selected.

No background colour changes happen, even though isSelected is true
when it should be. I wonder if I am missing something obvious.

package com.mindprod.vercheck;

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

/**
* render a the AppSate enum cell, use icon without text.
*/
final class AppStateRenderer implements TableCellRenderer
   {
   // ------------------------------ FIELDS
------------------------------

   private static final JLabel template = new JLabel( "",
JLabel.CENTER );

   private Color background;

   private Color selectedBackground;

   // -------------------------- PUBLIC INSTANCE  METHODS
--------------------------
   /**
    * constructor
    *
    * @param background         background colour
    * @param selectedBackground background colour when seleced
    */
   public AppStateRenderer( Color background, Color
selectedBackground )
       {
       this.background = background;
       this.selectedBackground = selectedBackground;
       }

   public Component getTableCellRendererComponent( JTable table,
                                                   Object value,
                                                   boolean
isSelected,
                                                   boolean hasFocus,
                                                   int row,
                                                   int column )
       {
       System.err.println( "selected:" + isSelected + " " + row + " "
+ table.getSelectedRow() );

       template.setIcon( ( (AppState) value ).getIcon() );
       template.setBackground( isSelected? selectedBackground :
background );
       
       return template;
       }
   }
Signature

Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com

Roedy Green - 12 Jan 2008 13:33 GMT
On Sat, 12 Jan 2008 13:18:26 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>final class AppStateRenderer implements TableCellRenderer

This is not sufficient. You must extend DefaultTableCellRenderer.
Signature

Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.