-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.3.0
-
x86
-
windows_2000
Name: boT120536 Date: 05/06/2001
java version "1.3.0"
Java(TM) 2 Runtime Environment. Standard Edition (build 1.3.0-c)
Hava HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
If a ListCellRendered is made of multiple components, then it needs to be
"validated" before it is painted. This validation DOES seem to be happening
when the combo box is "dropped-down," but it does NOT seem to be happening when
the combo box is in a "non-dropped-down" state. The following source code
exhibits the behavior. Resizing the frame and/or selecting various items
demonstrates the bug.
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
public class ComboTest {
public ComboTest() {
ComboFrame frame = new ComboFrame();
frame.validate();
frame.setVisible(true);
}
static public void main(String[] args) {
new ComboTest();
}
}
class ComboFrame extends JFrame {
public ComboFrame() {
Vector v = new Vector(4);
v.addElement(new TextPair("a","b"));
v.addElement(new TextPair("the quick brown fox","jumps"));
v.addElement(new TextPair("xxx","yyyyyyyyyyyyyyyy"));
v.addElement(new TextPair("foo","bar"));
JComboBox cb = new JComboBox();
cb.setRenderer(new ComboRenderer());
cb.setModel(new DefaultComboBoxModel(v));
cb.getModel().setSelectedItem(v.elementAt(1));
getContentPane().setLayout(new BorderLayout());
getContentPane().add("North",cb);
setSize(new Dimension(400, 300));
}
}
class ComboRenderer extends JComponent implements ListCellRenderer {
private JButton b1;
private JButton b2;
private Border noFocusBorder;
public ComboRenderer() {
b1 = new JButton();
b2 = new JButton();
setLayout(new GridBagLayout());
add(b1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
add(b2, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
noFocusBorder = new EmptyBorder(1, 1, 1, 1);
setOpaque(true);
setBorder(noFocusBorder);
}
public void setBackground(Color c) {
super.setBackground(c);
b1.setBackground(c);
b2.setBackground(c);
}
public void setForeground(Color c) {
super.setForeground(c);
b1.setForeground(c);
b2.setForeground(c);
}
public void setFont(Font f) {
super.setFont(f);
b1.setFont(f);
b2.setFont(f);
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setComponentOrientation(list.getComponentOrientation());
setEnabled(list.isEnabled());
setFont(list.getFont());
TextPair tp = (TextPair)value;
b1.setText(tp.text1);
b2.setText(tp.text2);
setBorder((cellHasFocus) ?
UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
return this;
}
}
class TextPair {
public String text1;
public String text2;
public TextPair(String s1,String s2) {
text1 = s1;
text2 = s2;
}
}
(Review ID: 123733)
======================================================================
- duplicates
-
JDK-4238829 JComboBox containing JPanel fails to display selected item at creation time
-
- Resolved
-