Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing an animated GIF in a JComboBox
    primarykey
    data
    text
    <p>I'm trying to use animated (GIF) icons in a JComboBox.</p> <p>As the DefaultListCellRenderer is based on JLabel, ImageIcons are directly supported when putting them into the the ComboBoxModel.</p> <p>However this does not work with animated GIFs.</p> <p>In the dropdown they are not show at all unless they are selected (the GIFs do work when used in a regular JLabel though)</p> <p>The code to populate the combobox is straight forward:</p> <pre><code>ImageIcon[] data = new ImageIcon[4]; data[0] = new ImageIcon("icon_one.gif"); data[1] = new ImageIcon("icon_two.gif"); data[2] = new ImageIcon("icon_three.gif"); data[3] = new ImageIcon("icon_four.gif"); ComboBoxModel model = new DefaultComboBoxModel(data); setModel(model); </code></pre> <p>icon_one.gif is the static one and is shown without any problems. The others are animated. (The images <strong>are</strong> loaded correctly because if I assign any of those icons to a JLabel directly they are displayed just fine)</p> <p>I also tried to use my own ListCellRenderer based on a JPanel (inspired by the answer to this question: <a href="https://stackoverflow.com/questions/5613120/java-animated-gif-without-using-a-jlabel">Java animated GIF without using a JLabel</a>). </p> <p>That works a <em>bit</em> better but not ideal either. The icons are only shown if I move the mouse over them while the dropdown is shown. So I guess it's a repaiting issue, although I don't know where</p> <p>This is the part from my JPanel that implements the ListCellRenderer interface.</p> <pre><code>public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { this.image = ((ImageIcon)value).getImage(); if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } revalidate(); repaint(); return this; } </code></pre> <p>The call to revalidate() and repaint() was inspired by looking at the code of JLabel.setIcon()</p> <p>The paint() method is straight forward as well: </p> <pre><code>public void paintComponent(Graphics g) { super.paintComponent(g); if (image != null) { g.drawImage(image, 0, 0, this); } } </code></pre> <p>Any ideas? I don't really need those icons to be animated in the dropdown (although that would be nice) but I would at least like to see the static images.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload