-
Bug
-
Resolution: Fixed
-
P4
-
1.1.6, 1.3.0
-
beta
-
generic, x86
-
generic, windows_2000
Name: krT82822 Date: 05/17/99
When a JLabel is used as a ListCellRenderer for JComboBox, at the creation time (the first display of the of the JComboBox on the application Frame) the editBox is filled with the first item in the list.
BUT when a JPANEL is used as a ListCellRenderer for JComboBox, the editBox is not filled at stratup time even when we use the instruction setSelectedIndex.
At the first use of the JComboBox (when you deploy the popup list) all the things turns well ...
How can I init my comboBox in the right way ?
The following code reproduce the related problem :
this code is derived from one used to fix the bug 4098249
I have used swing 1.1.1 beta 1 (the problem occurs also with swing 1.1.0)
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
class BrownListCell extends JPanel
{
String name;
public String toString() {
return "name="+name+super.toString();
}
public BrownListCell(String name) {
super();
setLayout(new GridLayout(1,3,5,5));
this.name = name;
JLabel l = new JLabel(name+",column1");
l.setOpaque(true);
add(l);
l = new JLabel(name+",column2");
l.setOpaque(true);
add(l);
l = new JLabel(name+",column3");
l.setOpaque(true);
add(l);
}
}
class BrownCellRenderer implements ListCellRenderer {
Hashtable comps = new Hashtable(10);
void addCell(Object key, Component val) {
comps.put(key, val);
}
public Component getListCellRendererComponent(JList l, Object k, int index,
boolean isSelected, boolean hasFocus)
{
Container c;
if ((c = (Container)comps.get(k)) == null) {
return null;
}
for(int i = 0; i < c.getComponentCount(); i++) {
Component child = c.getComponent(i);
if (isSelected) {
child.setForeground(Color.red);
child.setBackground(Color.blue);
} else {
child.setForeground(Color.black);
child.setBackground(Color.white);
}
}
return c;
}
}
class BrownCompoundCellTest extends Container
{
static String[] data = {
"Elem A", "Elem B", "Elem C"
};
BrownCompoundCellTest()
{
JComboBox combo = new JComboBox(data);
BrownCellRenderer rend = new BrownCellRenderer();
for (int i = 0; i < data.length; i++) {
Container lab = new BrownListCell(data[i]);
rend.addCell(data[i], lab);
}
combo.setBackground(Color.lightGray);
combo.setRenderer(rend);
setLayout(new BorderLayout());
add(combo, BorderLayout.CENTER);
combo.setSelectedIndex(0);
}
}
public class testCombo {
public static void main(String[] args)
{
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};
JFrame f = new JFrame("JCombo");
f.addWindowListener(l);
f.getContentPane().add(new BrownCompoundCellTest());
f.pack();
f.show();
}
}
(Review ID: 55053)
======================================================================
- duplicates
-
JDK-4455045 ListCellRenderer is NOT getting validated when displaying JComboBox
-
- Closed
-