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 2007

Tip: Looking for answers? Try searching our database.

Unexpected TableCellRenderer Behaviour

Thread view: 
Lorenzo - 25 Jan 2007 16:42 GMT
hi all,

I wrote a customized renderer for a JTable but it is behaving
differently from what I expected: even if I just acted on the
BACKground color, when this render is selected the table does not show
the content of the cells. Double-clicking on a cell shows the data
correctly sorted in it but, as soon as the cell is de-selected, data
re-become invisible.

Here is the code: can anybody please tell me what am I doing wrong?

lor

****************************************************************************************************
import java.awt.*;
import java.awt.event.*;
import java.awt.Transparency;
import java.lang.Object.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class UDP_JTable extends JTable
{
   public UDP_JTable(int rows, int cols)
   {
       super(rows, cols);
       for (int i=0; i<cols; i++)
       {
           getColumnModel().getColumn(i).setCellRenderer(new
UDRenderer());
       }
   }

   class UDRenderer extends DefaultTableCellRenderer implements
TableCellRenderer
   {
       public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int
column)
       {
           setForeground(Color.BLACK);
           if(row != column)
           {
               if (row>column)
               {
                   setBackground(Color.CYAN);
               }
               else
               {
                   setBackground(Color.WHITE);
               }
           }
           else
           {
               setBackground(Color.YELLOW);
           }
           return this;
       }
   }

   public static void main(String[] args)
   {      
   }
   
}
Jim Sculley - 26 Jan 2007 15:15 GMT
> hi all,
>
[quoted text clipped - 6 lines]
>
> Here is the code: can anybody please tell me what am I doing wrong?

The cell renderer is responsible for painting *everything* in the cell, including the contents.  Your renderer does
nothing but set background and foreground colors, so no content is painted.  Since your renderer extends
DefaultTableCellRenderer the solution is simple.  Just call the superclass getTableCellRendererComponent method before
performing your work:

super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
//do your stuff here
return this;

> lor
>
[quoted text clipped - 51 lines]
>    
> }

Signature

Remove my extraneous mandibular appendages to reply via email.



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.