Name: jk109818 Date: 12/16/2002
FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)
FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]
A DESCRIPTION OF THE PROBLEM :
When the DefaultListCellRenderer is used to display html
text, the cell is drawn with a larger height than the
necessary size to display the html text. In the code below,
when the JList is first display, the renderer is taller than
the actual html table. Note that when the button is pressed,
a new item is added to the JList model and at that point the
renderer will switch to the correct size.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the source code below in JDK.1.4.1
EXPECTED VERSUS ACTUAL BEHAVIOR :
The size of the renderer should be just enough to display
the html table. The renderer size should not change when a
new item is added to the JList.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
/**
* @author wchung
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class HTMLListTest extends JFrame
{
DefaultListModel model = new DefaultListModel();
/**
* Constructor for HtmlListRenderer.
*/
public HTMLListTest()
{
super();
setDefaultCloseOperation( EXIT_ON_CLOSE );
Container contentPane = getContentPane();
contentPane.setLayout( new BorderLayout() );
model.addElement( "AAAAAA AAAAAAAAA" );
model.addElement( "BBBBBB BBBBB" );
model.addElement( "CCCC CCCC" );
model.addElement( "DDDD DDDDD" );
model.addElement( "AAAAAA AAAAAAAAA" );
model.addElement( "BBBBBB BBBBB" );
model.addElement( "AAAAAA AAAAAAAAA" );
model.addElement( "BBBBBB BBBBB" );
model.addElement( "AAAAAA AAAAAAAAA" );
model.addElement( "BBBBBB BBBBB" );
model.addElement( "AAAAAA AAAAAAAAA" );
model.addElement( "BBBBBB BBBBB" );
JList htmlList= new JList( model );
htmlList.setCellRenderer( new HtmlRenderer() );
JScrollPane scroll = new JScrollPane( htmlList );
scroll.setPreferredSize( new Dimension( 100 , 500 ) );
JButton addButton = new JButton( "Add" );
contentPane.add( scroll , BorderLayout.CENTER );
pack();
//addNotify();
//setSize(getPreferredSize());
//validate();
//Object firstElement = model.elementAt( 0 );
//model.set( 0 , firstElement );
addButton.addActionListener( new AddActionListener() );
contentPane.add( addButton , BorderLayout.SOUTH );
}
class AddActionListener implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
model.addElement( "PPPPP PPPPPPPPPPPPP");
}
}
class HtmlRenderer extends JLabel implements ListCellRenderer
{
public Component getListCellRendererComponent( JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus)
{
setText( "<html><body><table bgcolor=\"yellow\" border=\"1\"><tr><td
align=\"center\" width=\" " +
list.getSize().width + " \">" +
( String )value +
"</td></tr></table></body></html>" );
setHorizontalAlignment( SwingConstants.CENTER );
return( this );
}
public void paintComponent( Graphics graphics )
{
super.paintComponent( graphics );
graphics.drawRect( 0 , 0 , getSize().width -1 , getSize().height - 1 );
}
}
public static void main(String[] args)
{
HTMLListTest frame = new HTMLListTest();
frame.pack();
frame.setVisible( true );
}
}
---------- END SOURCE ----------
CUSTOMER WORKAROUND :
The renderer will resize itself if the list model fire an
event to update the view after top level frame pack() method
is called.
(Review ID: 179163)
======================================================================