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 / February 2004

Tip: Looking for answers? Try searching our database.

Java TableCellRenderer for a boolean checkbox field

Thread view: 
Krick - 03 Feb 2004 22:51 GMT
I just wanted to post some code that I recently got working
for the benefit of other programmers who may have encountered
the same problems I did getting this to work.  Enjoy!

class CheckBoxTableCellRenderer
 extends JCheckBox
 implements TableCellRenderer {
 
 Border noFocusBorder;
 Border focusBorder;

 public CheckBoxTableCellRenderer() {
   super();
   setOpaque(true);
   setBorderPainted(true);
   setHorizontalAlignment(SwingConstants.CENTER);
   setVerticalAlignment(SwingConstants.CENTER);
 }

 public Component getTableCellRendererComponent(JTable table, Object value,
                                                boolean isSelected,
                                                boolean hasFocus,
                                                int row, int column) {

   if (isSelected) {
     setForeground(table.getSelectionForeground());
     setBackground(table.getSelectionBackground());
   }
   else {
     setForeground(table.getForeground());
     setBackground(table.getBackground());
   }

   if (hasFocus) {
     if (focusBorder == null) {
       focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
     }
     setBorder(focusBorder);
   }
   else {
     if (noFocusBorder == null) {
       noFocusBorder = new EmptyBorder(1, 1, 1, 1);
     }
     setBorder(noFocusBorder);
   }

   setSelected(Boolean.TRUE.equals(value));
   return this;
 }
}
Manish Hatwalne - 04 Feb 2004 07:23 GMT
If you return Boolean as the column class for your JTable model, this will
be done automatically by JTable.

- Manish

> I just wanted to post some code that I recently got working
> for the benefit of other programmers who may have encountered
[quoted text clipped - 46 lines]
>   }
> }
Krick - 04 Feb 2004 14:55 GMT
I'm not sure what you mean.  Please explain.

> If you return Boolean as the column class for your JTable model, this will
> be done automatically by JTable.
[quoted text clipped - 53 lines]
> >   }
> > }
Christian Kaufhold - 04 Feb 2004 15:51 GMT
> class CheckBoxTableCellRenderer
>  extends JCheckBox
[quoted text clipped - 6 lines]
>    super();
>    setOpaque(true);

Do not use setOpaque for buttons.

    setContentAreaFilled(true);

>    setBorderPainted(true);
>    setHorizontalAlignment(SwingConstants.CENTER);
[quoted text clipped - 14 lines]
>      setBackground(table.getBackground());
>    }

    setEnabled(table.isEnabled());
    setComponentOrientation(table.getComponentOrientation());


>    if (hasFocus) {
>      if (focusBorder == null) {
[quoted text clipped - 5 lines]
>      if (noFocusBorder == null) {
>        noFocusBorder = new EmptyBorder(1, 1, 1, 1);

How do you know the focusBorder has insets (1,1,1,1)?

        if (focusBorder == null)
            focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");

        if (focusBorder != null)
        {
            Insets n = focusBorder.getBorderInsets(this);
            noFocusBorder = new EmptyBorder(n);
        }
    }
       

Christian
Kleopatra - 06 Feb 2004 14:53 GMT
>   public Component getTableCellRendererComponent(JTable table, Object value,
>                                                  boolean isSelected,
[quoted text clipped - 5 lines]
>       setBackground(table.getSelectionBackground());
>     }

don't assume table != null. From the api doc:

public Component getTableCellRendererComponent(JTable table,
   Object value, boolean isSelected, [...]

Parameters:
 table - the JTable that is asking the renderer to draw; can be null

DefaultTableCellRenderer is similarly misbehaving - but that's no reason
not to do it correctly :-)

Greetings
Jeanette
Krick - 06 Feb 2004 21:19 GMT
Ok, here's a revised version incorporating all the suggestions people have made...

class CheckBoxTableCellRenderer
 extends JCheckBox
 implements TableCellRenderer {
 
 Border noFocusBorder;
 Border focusBorder;

 public CheckBoxTableCellRenderer() {
   super();
   //setOpaque(true);
   setContentAreaFilled(true);  // use this instead of setOpaque()
   setBorderPainted(true);
   setHorizontalAlignment(SwingConstants.CENTER);
   setVerticalAlignment(SwingConstants.CENTER);
 }

 public Component getTableCellRendererComponent(JTable table, Object value,
                                                boolean isSelected,
                                                boolean hasFocus,
                                                int row, int column) {
   
   if(table == null) {
     // ???
   }
   else {
     if (isSelected) {
       setForeground(table.getSelectionForeground());
       setBackground(table.getSelectionBackground());
     }
     else {
       setForeground(table.getForeground());
       setBackground(table.getBackground());
     }

     setEnabled(table.isEnabled());
     setComponentOrientation(table.getComponentOrientation());

     if (hasFocus) {
       if (focusBorder == null) {
         focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
       }
       setBorder(focusBorder);
     }
     else {
       if (noFocusBorder == null) {
         if (focusBorder == null) {
           focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
         }
         if (focusBorder != null) {
           Insets n = focusBorder.getBorderInsets(this);
           noFocusBorder = new EmptyBorder(n);
         }
       }
       setBorder(noFocusBorder);
     }

     setSelected(Boolean.TRUE.equals(value));
   }
   return this;
 }
}


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.