-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
1.4.2
-
x86
-
windows_2000
Name: jk109818 Date: 08/28/2003
FULL PRODUCT VERSION :
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)
FULL OS VERSION :
Microsoft Windows 2000 [Version 5.00.2195]
A DESCRIPTION OF THE PROBLEM :
I have a test program that populates a JList with a bunch of items. You can always visually see the items being added to the list, but sometimes after they are all added, the list will mysteriously be erased. This problem happens about 30-60% of the time. You may have to run my test program multiple times to see the bug manifest. I did write my own ListCellRenderer, which is fairly straightforward. I found that if I comment out the following lines in the ListCellRenderer, the bug never manifests:
Font fnt = list.getFont().deriveFont(Font.PLAIN);
setFont(fnt);
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run my test program multiple times. In less than 5 trials you should see the problem. Perhaps even on the first try.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The JList should be populated with data.
ACTUAL -
The JList is empty.
REPRODUCIBILITY :
This bug can be reproduced often.
---------- BEGIN SOURCE ----------
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ListboxBug extends JFrame {
public static void main(String[] args)
{
new ListboxBug("JList ListCellRenderer");
}
public ListboxBug(String title)
{
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList jList = new JList(new jxListBoxModel());
jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jList.setName("1115");
JScrollPane jsc = new JScrollPane((JList)jList,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
);
getContentPane().add(jsc, BorderLayout.CENTER);
jList.setCellRenderer(new jxBoxtabRenderer(null));
pack();
setSize(150, 295);
setVisible(true);
((jxListBoxModel)jList.getModel()).removeAllElements();
minsert(jList, "BRNMNT ,CMNMNT ,COMPMNT ,CONSOLE ,CPRCMNT ,CTRLMNT ,CUSTMNT ,DBDMNT ,DCRMNT ,DIAGMNT ,ECLSMNT ,ELCMNT ,EOIMNT ,EQPMNT ,ESTSMNT ,FACMNT ,FUNTMNT ,GRPMNT ,ICLSMNT ,IDMNT ,ITEMMNT ,JOBMGR ,MFRMNT ,OCLSMNT ,OKITEST ,PAYRMNT ,PCLSMNT ,PCRDMNT ,PHYSMNT ,PINSMNT ,PLMNT ,PRNTEST ,PROCMNT ,PROGMNT ,PROVMNT ,PSPCMNT ,REFMNT ,REGMNT ,RTEMNT ,SIDMNT ,SLINMNT ,SREPMNT ,TERMMNT ,TERRMNT ,VENDMNT ,VPRCMNT ,WHSEMNT ");
JOptionPane.showMessageDialog(null, "There are " + jList.getModel().getSize() + " items in the list", "info", JOptionPane.INFORMATION_MESSAGE);
}
private void minsert(JComponent c1, String list)
{
StringBuffer sb1 = new StringBuffer(list);
for (int i1 = 0, i2 = 0; i1 < sb1.length(); i1++) {
if (sb1.charAt(i1) == ',' || (i1 + 1) == sb1.length()) {
if ((i1 + 1) == sb1.length()) i1++;
list = sb1.substring(i2, i1);
((jxListBoxModel)((JList)c1).getModel()).addElement(list);
i2 = i1 + 1;
}
}
}
// Inner class
class jxListBoxModel extends DefaultListModel {
public void addElement(Object anObject)
{
int i1, i2;
String newItem = (String)anObject;
i2 = getSize();
for (i1 = 0; i1 < i2; i1++) {
String s = (String)getElementAt(i1);
if (newItem.compareTo(s) > 0) continue;
insertElementAt(newItem, i1);
break;
}
if (i1 == i2) {
super.addElement(anObject);
}
}
}
// Inner class
class jxBoxtabRenderer extends JLabel implements ListCellRenderer {
private Color c1 = new Color(0, 0, 0); // black
private StringBuffer s1 = new StringBuffer();
private EmptyBorder eborder = new EmptyBorder(1, 1, 1, 1);
jxBoxtabRenderer(String[] width)
{
super();
setOpaque(true);
setBorder(new EmptyBorder(1, 1, 1, 1));
}
public Component getListCellRendererComponent(
JList list,
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // the list and the cell have the focus
{
int i2, totalsize = 0;
String s2;
jxListBoxModel jlbm = (jxListBoxModel) list.getModel();
if (value == null) {
setText("");
return this;
}
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else {
setForeground(c1);
setBackground(list.getBackground());
}
//////////////////////////////////////
// Comment these two lines out, and program works great every time
Font fnt = list.getFont().deriveFont(Font.PLAIN);
setFont(fnt);
///////////////////////////////////////
s2 = ((String) value).toString();
setEnabled(list.isEnabled());
setBorder((cellHasFocus) ? UIManager.getBorder("List.focusCellHighlightBorder") : eborder);
setText(s2);
return this;
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Do not call Font.deriveFont() for the JList in ListCellRenderer.
(Incident Review ID: 199327)
======================================================================