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 / June 2007

Tip: Looking for answers? Try searching our database.

Why there is no setCellRenderer in JTable?

Thread view: 
RC - 26 Jun 2007 19:48 GMT
Hi there,

So far I only found

JTable.setDefaultRenderer(Class, TableCellRenderer);

I would like set foreground color in 1st column of my table,
and also want to detected the selection listener.

Here are what I did as following, No errors, but nothing
is work.

Please help.
Thank Q!

table.setDefaultRenderer(Color.class,
      new LabelDefaultTableCellRenderer());

public class LabelDefaultTableCellRenderer extends JLabel
implements TableCellRenderer {

    private static final long serialVersionUID = 1L;

    public LabelDefaultTableCellRenderer() {
        super();
        setOpaque(true);
    }

    public Component getTableCellRendererComponent(JTable table,
                                               Object color,
                                               boolean isSelected,
                                               boolean hasFocus,
                                               int row,
                                               int column) {
        Color newColor = (Color)color;
       
        if (column == 1) {
            setForeground(Color.red);
           
        }   

        if (isSelected) {
            System.out.println("Cell selected at " +
                                       row + ", " + column);
        }
        return (this);
    }
}
Adrian Hands, Raleigh NC  USA - 26 Jun 2007 19:56 GMT
> public class LabelDefaultTableCellRenderer extends JLabel

no, don't extend JLabel

>         public Component getTableCellRendererComponent(JTable table,
>                                                 Object color,
[quoted text clipped - 16 lines]
>
>                 return comp;
RC - 26 Jun 2007 20:08 GMT
Adrian Hands, Raleigh NC USA wrote:

>> public class LabelDefaultTableCellRenderer extends JLabel
>
> no, don't extend JLabel

Then what I should extends?

I got the example from this page

http://java.sun.com/docs/books/tutorial/uiswing/examples/components/TableDialogE
ditDemoProject/src/components/ColorRenderer.java


>>         public Component getTableCellRendererComponent(JTable table,
>>                                                 Object color,
[quoted text clipped - 16 lines]
>>
>>                 return comp;
Adrian Hands, Raleigh NC  USA - 26 Jun 2007 20:34 GMT
public class Trash
{
   public static void main( String[] args )
   {
       javax.swing.JTable table = new javax.swing.JTable( 5, 5 );
       table.setDefaultRenderer( Object.class, new
LabelDefaultTableCellRenderer() );

       javax.swing.JFrame frame = new javax.swing.JFrame();
       frame.getContentPane().add( table );
       frame.pack();
       frame.setVisible( true );
   }

   public static class LabelDefaultTableCellRenderer
       extends javax.swing.table.DefaultTableCellRenderer
   {

       private static final long serialVersionUID = 1L;

       public LabelDefaultTableCellRenderer() {
           super();
           setOpaque(true);
       }

       public java.awt.Component
getTableCellRendererComponent( javax.swing.JTable table,

Object color,

boolean isSelected,

boolean hasFocus,
                                                                int
row,
                                                                int
column ) {

           java.awt.Component comp =
               super.getTableCellRendererComponent( table,
                                                    color,
                                                    isSelected,
                                                    hasFocus,
                                                    row,
                                                    column );

           // Color newColor = (Color)color;

           // note: column 1 is the second column
           comp.setForeground( column == 1 ? java.awt.Color.red :
java.awt.Color.black );

           // note: this gets written every time the cell is
rendered, e.g. when it's uncovered, etc--very often!
           if (isSelected) {
               System.out.println("Cell selected at " +
                                  row + ", " + column);
           }
           return comp;
       }

   }

}
Tom Hawtin - 26 Jun 2007 20:12 GMT
> public class LabelDefaultTableCellRenderer extends JLabel

That's an odd class name. DefaultTableCellRenderer is so called, because
it's the implementation you get by default.

I wouldn't subclass JLabel. You don't need to subclass it, so having a
reference to a label is probably a better idea. Although
DefaultTableCellRenderer takes a different view.

The common thing to do is subclass DefaultTableCellRenderer.

> implements TableCellRenderer {
>
[quoted text clipped - 17 lines]
>            
>         }  

And if the column isn't 1? Most (but not all) if statements should
either end in return/break/continue/throw or have an else clause.

>         if (isSelected) {
>             System.out.println("Cell selected at " +
[quoted text clipped - 3 lines]
>     }
> }

The obvious thing you are failing to do is set the text of the label.
Without any text, you wont see the change of foreground very well.

Tom Hawtin

[Note: Followup-To: comp.lang.java.gui]
Roedy Green - 29 Jun 2007 01:20 GMT
Why there is no setCellRenderer in JTable? you ask.

It is very rare that the renderers for all columns are identical.  To
create such a beast, just loop over all columns.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Tom Hawtin - 29 Jun 2007 09:56 GMT
> Why there is no setCellRenderer in JTable? you ask.

The documentation for getDefaultRenderer seems to imply that you could
set a default renderer for all columns by setting the default renderer
for Object.class, Number.class and Boolean.class.

Looking at the source, you would also need Float.class, Double.class,
Date.class, Icon.class, and ImageIcon.class. Gotta love Swing.

Tom Hawtin

[Followup-to: comp.lang.java.gui]
Tom Hawtin - 29 Jun 2007 11:16 GMT
>> Why there is no setCellRenderer in JTable? you ask.
>
[quoted text clipped - 4 lines]
> Looking at the source, you would also need Float.class, Double.class,
> Date.class, Icon.class, and ImageIcon.class. Gotta love Swing.

Actually, a more reliable way of doing it is to subclass JTable and
clear the protected defaultEditorsByColumnClass (UIDefaults) Hashtable.

Tom Hawtin


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.