2012-03-04 64 views
2

我正嘗試在JComboBox中使用動畫(GIF)圖標。在JComboBox中使用動畫GIF

由於DefaultListCellRenderer基於JLabel,因此在將它們放入ComboBoxModel時直接支持ImageIcons。

但是,這不適用於動畫GIF。

,除非它們被選中(在常規JLabel的使用雖然當的GIF做的工作)

填充組合框的代碼,他們沒有在所有顯示的下拉是直截了當:

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); 

icon_one.gif是靜態的,沒有任何問題。其他人是動畫。 (正確加載,因爲如果我給你任何的這些圖標到JLabel直接顯示這些就好了圖像)

我還試圖用基於一個JPanel(由回答啓發,這個問題我自己的ListCellRenderer :Java animated GIF without using a JLabel)。

這是一個更好但不理想。只有在顯示下拉菜單時將鼠標移到它們上方時,纔會顯示圖標。所以我想這是一個修復問題,雖然我不知道在哪裏

這是從我的JPanel實現ListCellRenderer接口的一部分。

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; 
} 

調用重新驗證()和重繪()通過查看JLabel.setIcon()的代碼啓發

的paint()方法是直截了當,以及:

public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    if (image != null) 
    { 
    g.drawImage(image, 0, 0, this); 
    } 
} 

任何想法?我並不需要這些圖標在下拉菜單中進行動畫製作(儘管這樣會很好),但我至少希望看到靜態圖像。

+0

啓發見http://stackoverflow.com/questions/575782/how-to-display-animation-in-a-jtable-cell了類似的問題。 – 2012-03-04 17:10:12

回答

2

這個例子是從AnimatedIconTableExample.java

import java.awt.*; 
import java.awt.image.*; 
import java.net.*; 
import javax.swing.*; 
import javax.swing.plaf.basic.*; 
class MainPanel { 
    public JComponent makeUI() { 
    JComboBox combo = new JComboBox(); 
    URL url1 = getClass().getResource("static.png"); 
    URL url2 = getClass().getResource("animated.gif"); 
    combo.setModel(new DefaultComboBoxModel(new Object[] { 
     new ImageIcon(url1), makeImageIcon(url2, combo, 1) 
    })); 
    JPanel p = new JPanel(); 
    p.add(combo); 
    return p; 
    } 
    private static ImageIcon makeImageIcon(
     URL url, final JComboBox combo, final int row) { 
    ImageIcon icon = new ImageIcon(url); 
    icon.setImageObserver(new ImageObserver() { 
     //http://www2.gol.com/users/tame/swing/examples/SwingExamples.html 
     //AnimatedIconTableExample.java 
     @Override public boolean imageUpdate(
      Image img, int infoflags, int x, int y, int w, int h) { 
     if(combo.isShowing() && (infoflags & (FRAMEBITS|ALLBITS)) != 0) { 
      if(combo.getSelectedIndex()==row) { 
      combo.repaint(); 
      } 
      BasicComboPopup p = (BasicComboPopup) 
      combo.getAccessibleContext().getAccessibleChild(0); 
      JList list = p.getList(); 
      if(list.isShowing()) { 
      list.repaint(list.getCellBounds(row, row)); 
      } 
     } 
     return (infoflags & (ALLBITS|ABORT)) == 0; 
     }; 
    }); 
    return icon; 
    } 
    public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override public void run() { 
     createAndShowGUI(); 
     } 
    }); 
    } 
    public static void createAndShowGUI() { 
    JFrame f = new JFrame(); 
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    f.getContentPane().add(new MainPanel().makeUI()); 
    f.setSize(320, 240); 
    f.setLocationRelativeTo(null); 
    f.setVisible(true); 
    } 
} 
+0

謝謝你完美的作品。 – 2012-03-04 23:20:12

+0

+1不錯,哪裏出生'(infoflags&(FRAMEBITS | ALLBITS))' – mKorbel 2012-03-09 00:27:04

+0

@mKorbel引用自:src/java/awt/Component.java#imageUpdate(...) – aterai 2012-03-13 09:08:17