Skip to main content

HTML in DefaultTableRenderer

Please note these forums are being decommissioned and use the new and improved forums at
No replies
Joined: 2006-08-14

Hi all,

I am struggling with a strange problem related to the use of HTML text in DefaulTableRenderer (the DefaultTableCellRenderer works fine).

When I try to renderer cells surrounded with HTML tags something fishy happens with the foreground. With the windows system lnf, the foreground for selected cells should be white (and the background blue). When a table contains identical cells, it seems like the first (top-down) cell decides which foreground is used.

In a table like this:

1 Text

2 Text

3 Text2

4 Text2

5 Text2

The foreground of Text2 (for rows 3, 4 and 5) will be determined by row 3. If I select row 3, the foreground for cells 3,4 and 5 will be white, even if only row 3 is selected. This means the text on rows 4 and 5 disappears, as both foreground and background is white.

The following example illustrates this problem.

<p>import javax.swing.JFrame;<br />
import javax.swing.JScrollPane;<br />
import javax.swing.UIManager;<br />
import javax.swing.UnsupportedLookAndFeelException;<br />
import javax.swing.table.DefaultTableModel;</p>
<p>import org.jdesktop.swingx.JXTable;</p>
<p>public class TestHTML {<br />
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {<br />
        UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );</p>
<p>        DefaultTableModel model = new DefaultTableModel( new Object[][]{<br />
            {"<HTML>a</HTML>"},<br />
            {"<HTML>a</HTML>"},<br />
            {"<HTML>b</HTML>"},<br />
            {"<HTML>b</HTML>"},<br />
            {"<HTML>b</HTML>"}}, new Object[]{"Header"});</p>
<p>        JXTable table = new JXTable( model );</p>
<p>        JFrame frame = new JFrame();<br />
        frame.getContentPane().add( new JScrollPane( table ));</p>
<p>        frame.pack();<br />
        frame.setVisible(true);<br />
    }<br />
}<br />

Select the third row. Move the mouse cursor up and down over all the rows to make the table repaint the cells.

I'd say the HTML view is cached somehow, but I don't understand why it should happen with DefaultCellRenderer but not with DefaultTableCellRenderer.

Any ideas?