# Sun tutorial code von jcombobox mit image im combobox fehlt!



## Razor1911 (2. Nov 2007)

Hallo,

ich suche den source code zu diesem bild:

http://java.sun.com/docs/books/tutorial/figures/uiswing/components/CustomComboBoxDemoMetal.png

das Bild ist von den Sun java swing tutorials über JComboBoxen.

Unter dem Bild befindet sich ein link auf eine .java Datei, doch diese Datei beinhaltet NICHT den richtigen Quellcode, sondern Code dass das Bild außerhalb der JCombobox dargestellt wird. 

falscher code: http://java.sun.com/docs/books/tuto...xDemoProject/src/components/ComboBoxDemo.java

Wo ist die .java datei mit dem Code wo das Bild in der Combobox ist?


----------



## Razor1911 (2. Nov 2007)

Razor1911 hat gesagt.:
			
		

> Hallo,
> 
> ich suche den source code zu diesem bild:
> 
> ...



komisch fehlt doch net    ???:L


----------



## Razor1911 (2. Nov 2007)

ok habe mir den Code mal ausführbar... zusammengebastelt wie ich es möchte, doch bekomme ich einen Nullpointer  Exception, warum?

Diese Zeile:

```
int selectedIndex = ((Integer)value).intValue();
```

wird als fehlerhaft angezeigt, doch ich versteh net warum?!


```
import java.awt.Component; 
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

public class CustomComboBoxDemo extends JFrame
{     
	private static final long serialVersionUID = 1L;
	ImageIcon[] images;
    String[] petStrings = {"cd", "network"};    
    Integer[] intArray = new Integer[petStrings.length];
    JComboBox petList = new JComboBox(intArray);
    
    public CustomComboBoxDemo()
    {
        super("test");
        
        setLayout(null);
        add(petList);
        ComboBoxRenderer renderer= new ComboBoxRenderer();
        petList.setBounds(10,10,200,50);
        petList.setRenderer(renderer);  
        
        images = new ImageIcon[petStrings.length]; 
        
        for (int i = 0; i < petStrings.length; i++)
        {
            intArray[i] = new Integer(i);
            images[i] = createImageIcon(petStrings[i] + ".png");
            if (images[i] != null)
            {
                images[i].setDescription(petStrings[i]);
            }
        }
    }

    protected static ImageIcon createImageIcon(String path)
    {
        URL imgURL = CustomComboBoxDemo.class.getResource(path);
        if (imgURL != null)
        {
            return new ImageIcon(imgURL);
        }
        else
        {
            System.err.println("Couldn't find file: " + path);
                return null;
        }
    }
   
    public static void main(String[] args)
    { 
    	CustomComboBoxDemo frame = new CustomComboBoxDemo();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);                
        frame.setVisible(true);
    }

    class ComboBoxRenderer extends JLabel  implements ListCellRenderer 
    { 
		private static final long serialVersionUID = 1L;

		public ComboBoxRenderer()
        {
            setOpaque(true);           
        }
       
        public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus)
        {
           
            int selectedIndex = ((Integer)value).intValue();

            if (isSelected)
            {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            }
            else 
            {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
            
            ImageIcon icon = images[selectedIndex];
            String pet = petStrings[selectedIndex];
            setIcon(icon);
            
            if (icon != null)
            {
                setText(pet);                
            }
            else
            {
                setText(pet + " no image available");
                            
            }

            return this;
        }
    }
}
```


----------



## Razor1911 (3. Nov 2007)

Sorry habe die Fehlermeldung vergessen...

```
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at CustomComboBoxDemo$ComboBoxRenderer.getListCellRendererComponent(CustomComboBoxDemo.java:75)
	at javax.swing.plaf.basic.BasicComboBoxUI.paintCurrentValue(Unknown Source)
	at javax.swing.plaf.metal.MetalComboBoxUI.paintCurrentValue(Unknown Source)
	at javax.swing.plaf.basic.BasicComboBoxUI.paint(Unknown Source)
	at javax.swing.plaf.metal.MetalComboBoxUI.paint(Unknown Source)
	at javax.swing.plaf.ComponentUI.update(Unknown Source)
	at javax.swing.JComponent.paintComponent(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JLayeredPane.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
	at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
	at javax.swing.RepaintManager.paint(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
	at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
	at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
	at java.awt.Container.paint(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at CustomComboBoxDemo$ComboBoxRenderer.getListCellRendererComponent(CustomComboBoxDemo.java:75)
	at javax.swing.plaf.basic.BasicComboBoxUI.paintCurrentValue(Unknown Source)
	at javax.swing.plaf.metal.MetalComboBoxUI.paintCurrentValue(Unknown Source)
	at javax.swing.plaf.basic.BasicComboBoxUI.paint(Unknown Source)
	at javax.swing.plaf.metal.MetalComboBoxUI.paint(Unknown Source)
	at javax.swing.plaf.ComponentUI.update(Unknown Source)
	at javax.swing.JComponent.paintComponent(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
	at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
	at javax.swing.RepaintManager.paint(Unknown Source)
	at javax.swing.JComponent._paintImmediately(Unknown Source)
	at javax.swing.JComponent.paintImmediately(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at CustomComboBoxDemo$ComboBoxRenderer.getListCellRendererComponent(CustomComboBoxDemo.java:75)
	at javax.swing.plaf.basic.BasicComboBoxUI.paintCurrentValue(Unknown Source)
	at javax.swing.plaf.metal.MetalComboBoxUI.paintCurrentValue(Unknown Source)
	at javax.swing.plaf.basic.BasicComboBoxUI.paint(Unknown Source)
	at javax.swing.plaf.metal.MetalComboBoxUI.paint(Unknown Source)
	at javax.swing.plaf.ComponentUI.update(Unknown Source)
	at javax.swing.JComponent.paintComponent(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
	at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
	at javax.swing.RepaintManager.paint(Unknown Source)
	at javax.swing.JComponent._paintImmediately(Unknown Source)
	at javax.swing.JComponent.paintImmediately(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
```


----------

