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

Tip: Looking for answers? Try searching our database.

Custom JTable colum renderer eat up CPU resources

Thread view: 
Michael - 23 Mar 2004 01:43 GMT
My following codes causes the CPU usage to 100% all the time:

     CustomTableHeaderRenderer renderer = new CustomTableHeaderRenderer();

     TableColumn col1 = this.getColumnModel().getColumn( 0 );
     TableColumn col2 = this.getColumnModel().getColumn( 1 );
     col1.setHeaderRenderer( renderer );
     col2.setHeaderRenderer( renderer );
     this.setDefaultRenderer( this.getClass(), renderer );

What could be the reason for that? And how do I go about solving it?

TIA.
Christian Kaufhold - 23 Mar 2004 13:37 GMT
> My following codes causes the CPU usage to 100% all the time:
>
[quoted text clipped - 7 lines]
>
> What could be the reason for that? And how do I go about solving it?

What is CustomTableHeaderRenderer (code)? Why is it used for normal
cells as well? Why does your TableModel contain (presumably) JTables?

Christian
Signature

And in short, I was afraid.

Michael - 31 Mar 2004 05:52 GMT
> > My following codes causes the CPU usage to 100% all the time:
> >
[quoted text clipped - 12 lines]
>
> Christian

Hi Christian,

My CustomTableHeaderRenderer() does the following:

  public class CustomTableHeaderRenderer extends JLabel implements
TableCellRenderer {
     public Component getTableCellRendererComponent( JTable table,
Object value,
           boolean isSelected, boolean hasFocus, int rowIndex, int
vColIndex ) {
        // 'value' is column header value of column 'vColIndex'
        // rowIndex is always -1
        // isSelected is always false
        // hasFocus is always false

        // Configure the component with the specified value
        setText( value.toString() );

        // Set tool tip if desired
//         setToolTipText( ( String ) value );

        JTableHeader header = table.getTableHeader();
//         header.setBackground( HEADER_COLOR );

        // Disable the column-swapping feature
        header.setReorderingAllowed( false );
        header.setFont( new Font( "Tahoma", Font.BOLD, 12 ) );
//         header.setResizingAllowed( false );

        // This somehow removed the border lines
//         setBorder( UIManager.getBorder(
"Table.focusCellHighlightBorder" ) );

        if ( table.isCellEditable( rowIndex, vColIndex ) ) {
           setForeground( UIManager.getColor(
"Table.focusCellForeground" ) );
           setBackground( UIManager.getColor(
"Table.focusCellBackground" ) );
        }

//         this.revalidate();

        // Since the renderer is a component, return itself
        return this;
     }

Yes, my TableModel contains JTable.
Christian Kaufhold - 31 Mar 2004 15:02 GMT

>   public class CustomTableHeaderRenderer extends JLabel implements
> TableCellRenderer {

>      public Component getTableCellRendererComponent( JTable table,
> Object value,
[quoted text clipped - 17 lines]
>         header.setReorderingAllowed( false );
>         header.setFont( new Font( "Tahoma", Font.BOLD, 12 ) );

These lines have no business here. Move them to where the table/header
is created. In the renderer code, pick up the settings of the header, or
it won't have any effect:

         setFont(header.getFont());

(and also,

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

    >         if ( table.isCellEditable( rowIndex, vColIndex ) ) {
>            setForeground( UIManager.getColor(
> "Table.focusCellForeground" ) );
>            setBackground( UIManager.getColor(
> "Table.focusCellBackground" ) );
>         }

This is pointless and wrong for a header renderer.
Header cells are never editable.

Christian
Signature

http://www.chka.de/swing/table/faq.html

Michael - 01 Apr 2004 02:28 GMT
>  
> >   public class CustomTableHeaderRenderer extends JLabel implements
[quoted text clipped - 45 lines]
>
> Christian

Thanks, but how do I disable column swapping?
Christian Kaufhold - 01 Apr 2004 12:11 GMT

> Thanks, but how do I disable column swapping?

JTable t = new JTable(...);

t.getTableHeader().setReorderingAllowed(false);

Christian


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.