Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5011622

Default JComboBox renderer is not reset before rendering another data

XMLWordPrintable

      If a JComboBox has both Text and Image only entries, the renderer shows both of them combined as if each entry has both text and image. The renderer should be reset before rendering next item. This works fine for JList.

      Refer to attached screenshot.
      Compile and run JComboBoxTest.java
      Adding the test (JComboBoxTest.java) here so it is visible to the
      community on jdk.dev.java.net

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.util.Vector;

      public class JComboBoxTest {

          private static final String metal =
              "javax.swing.plaf.metal.MetalLookAndFeel";
          private static final String motif =
              "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
          private static final String windowsXP =
              "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
          private static final String windowsClassic =
              "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
          private static final String gtk =
              "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";

          public static void main(String args[]) throws Exception {

              //UIManager.setLookAndFeel(windowsXP);

              JFrame frame = new JFrame();
              frame.getContentPane().setLayout(new FlowLayout());

              JComboBox comboBox = new JComboBox();
              for(int i=0; i<10; i++) {
                  comboBox.addItem(new CBIcon1());
                  comboBox.addItem(new CBIcon2());
                  comboBox.addItem("Text Only! No Icon Please!! " + i);
              }
              frame.getContentPane().add(comboBox);

              Vector vector = new Vector();
              for(int i=0; i<10; i++) {
                  vector.addElement(new CBIcon1());
                  vector.addElement(new CBIcon2());
                  vector.addElement("Text Only! No Icon Please!! " + i);
              }
              JList list = new JList(vector);
              frame.getContentPane().add(new JScrollPane(list));

              frame.pack();
              frame.setVisible(true);
          }

          static class CBIcon1 implements Icon {

              public void paintIcon(Component c, Graphics g, int x, int y ) {

                  g.setColor(Color.yellow);
                  g.fillRect(0, 0, 202, 22);

                  for(int i=1; i<20; i+=2) {
                      g.setColor(Color.red);
                      g.drawRect(i, i, 202 - (i*2), 22 - (i*2));
                  }
              }

              public int getIconWidth() {
                  return 202;
              }

              public int getIconHeight() {
                  return 22;
              }
          }

          static class CBIcon2 implements Icon {

              public void paintIcon(Component c, Graphics g, int x, int y ) {

                  g.setColor(Color.blue);
                  g.fillRect(0, 0, 202, 22);

                  for(int i=1; i<20; i+=2) {
                      g.setColor(Color.yellow);
                      g.drawRect(i, i, 202 - (i*2), 22 - (i*2));
                  }
              }

              public int getIconWidth() {
                  return 202;
              }

              public int getIconHeight() {
                  return 22;
              }
          }
      }

            Unassigned Unassigned
            rmenonsunw Rakesh Menon (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: