   
Groups    
 Advanced Groups Search    Preferences    Groups Help  
  
  
Groups search result 7 for JTable DefaultTableCellRenderer setValue(   
 
 
 Intuitive Java Displays  Build visually rich, highly Interactive web-based interfaces  www.ilog.com Sponsored Links 
 
 BX for Java GUI Builder  The quick and easy way to build Swing/AWT user interfaces  www.ics.com 
 JFreeReport  Open source report writer for Java Includes export to PDF!  www.object-refinery.com 
 
Search Result 7 
From: noreplyALBatCWA@hotmail.com (noreplyALBatCWA@hotmail.com)
Subject: Re: JTable Rendering 
View: Complete Thread (7 articles)  
Original Format 
Newsgroups: comp.lang.java.gui
Date: 1998/05/28 
 

In article <355C71CF.BF41B9DE@us.ibm.com>,
  Deborah Lynn Franzen <dfranzen@us.ibm.com> wrote:
>
> I created a JTable with JLabels, but it won't display the JLabel or its
> Icon. I know I need to replace the renderer, but the docs on this
> subject don't really explain how to do this. PLEASE HELP!!! Thanks in
> advance!
>
>


I have had the same problem with displaying JLabels in a JTable object.
I solved it by creating a new CellRenderer.  Here's how:

Step 1:

Define a class JLabelRenderer which extends DefaultTableCellRenderer.
DefaultTableCellRenderer is derived from JLabel which makes the job easier.

import com.sun.java.swing.*;
import com.sun.java.swing.table.*;

public class JLabelRenderer extends DefaultTableCellRenderer
{
    public void setValue(Object value)
    {
        if (value instanceof JLabel)
        {
            setText(((JLabel)value).getText());
            setIcon(((JLabel)value).getIcon());
        }
    }
}


Step 2:
Define your own tablemodel which extends DefaultTableModel,
and modify the

import java.awt.*;
import com.sun.java.swing.*;
import com.sun.java.swing.table.AbstractTableModel;

public class TOCTableModel extends AbstractTableModel
{


    JLabel unitGraphic[] = new JLabel[...];

    // TODO: Initialize unitGraphic[]


    final static int JLABEL_COLUMN = 0;

    // TODO: determine size of JLabel dynamically
    final static int JLABEL_HEIGHT = 30;

    //...

    public Object getValueAt(int row, int col)
    {
        //You should probably clone() if unitGraphic[row] will be modified.
        if (col == JLABEL_COLUMN)
            return unitGraphic[row];
        //...
        return (Object)null;
    }

    //...

    public Class getColumnClass(int column)
    {
        if (column == 0)
            return JLabel.class;
        //...
        return (Class)null;

    }

    //...
}


Step 3:

In the code which initializes an instance of JTable,
add the JLabelRenderer to the list of default renderers.
Don't forget to set the row height, since this isn't done
automatically.

//...

    JLabelRenderer jLabelRenderer = new JLabelRenderer();

    DefenderjTable.setDefaultRenderer(JLabel.class, jLabelRenderer);
    DefenderjTable.setRowHeight(TOCTableModel.JLABEL_HEIGHT);

//...


I hope this helps.

Anders Beitnes

If you have any questions or comments, you can reach me at:
albATcwaDOTcom
(replace the AT with @, and the DOT with .)

CWA is a consulting house which designs software and hardware,
including embedded systems, usually for telephony/carrier/RBOC
and their vendors.



-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading


--------------------------------------------------------------------------------
Google Home - Advertise with Us - Business Solutions - Services & Tools - Jobs, Press, & Help 

2003 Google