Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2011
    Posts
    23
    Rep Power
    0

    Lightbulb Custom JComboBox

    Hello ,

    I am trying to create custom JComboBox to show image with the label. I found the code and i modified as follows.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package jcomboboximage;
    
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Font;
    import javax.swing.ImageIcon;
    import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.ListCellRenderer;
    
    /**
     *
     * @author vns08103
     */
    public class CustomComboBox extends JComboBox{
        private String[] strArrData = null;
        private String strImgFolderPath = null;
        private ImageIcon[] arrImgIcon = null;
        private String strImgExtension = ".png";
        public CustomComboBox(String[] strArrData, String strImgFolderPath)
        {
            super();
            this.strArrData = new String[strArrData.length];
            this.arrImgIcon = new ImageIcon[strArrData.length];
            this.strArrData = strArrData;
            this.strImgFolderPath = strImgFolderPath;
            
            Integer[] intArray = new Integer[strArrData.length];
            for (int i = 0; i < strArrData.length; i++) {
                intArray[i] = new Integer(i);
                arrImgIcon[i] = createImageIcon(strImgFolderPath+"/"+strArrData[i]+strImgExtension);
                if (arrImgIcon[i] != null) {
                    arrImgIcon[i].setDescription(strArrData[i]);
                }
            }
              
            setRenderer(new ComboBoxRenderer());
            setMaximumRowCount(3);
        }
        protected static ImageIcon createImageIcon(String path) { 
            System.out.println(path);
            java.net.URL imgURL = CustomComboBox.class.getResource(path);
            System.out.println(imgURL.getPath());
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + imgURL);
                    return null;
            }
        }
       ////////////
        
        class ComboBoxRenderer extends JLabel implements ListCellRenderer {
            private Font uhOhFont;
    
            public ComboBoxRenderer() {
                setOpaque(true);
                setHorizontalAlignment(CENTER);
                setVerticalAlignment(CENTER);
            }
    
            /*
             * This method finds the image and text corresponding
             * to the selected value and returns the label, set up
             * to display the text and image.
             */
            public Component getListCellRendererComponent(
                                               JList list,
                                               Object value,
                                               int index,
                                               boolean isSelected,
                                               boolean cellHasFocus) {
                //Get the selected index. (The index param isn't
                //always valid, so just use the value.)
                
               [B] int selectedIndex = ((Integer)value).intValue();[/B]
    
                if (isSelected) {
                    setBackground(list.getSelectionBackground());
                    setForeground(list.getSelectionForeground());
                } else {
                    setBackground(list.getBackground());
                    setForeground(list.getForeground());
                }
    
                //Set the icon and text.  If icon was null, say so.
                ImageIcon icon = arrImgIcon[selectedIndex];
                String pet = strArrData[selectedIndex];
                setIcon(icon);
                if (icon != null) {
                    setText(pet);
                    setFont(list.getFont());
                } else {
                    setUhOhText(pet + " (no image available)",
                                list.getFont());
                }
    
                return this;
            }
    
            //Set the font and text when no image was found.
            protected void setUhOhText(String uhOhText, Font normalFont) {
                if (uhOhFont == null) { //lazily create this font
                    uhOhFont = normalFont.deriveFont(Font.ITALIC);
                }
                setFont(uhOhFont);
                setText(uhOhText);
            }
        }
    
        //////////////
        
    }
    I want to use it as

    Java Code:
    String[] data = new String[1];
    String path = "/images";
    data[0] = "about";
    jComboBox1 = new CustomComboBox(data, path);
    
    jComboBox1.setName("jComboBox1"); // NOI18N
    But i am getting following error.

    Java Code:
    /images/about.png
    /C:/Documents%20and%20Settings/vns08103/My%20Documents/NetBeansProjects/JcomboBoxImage/build/classes/images/about.png
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at jcomboboximage.CustomComboBox$ComboBoxRenderer.getListCellRendererComponent(CustomComboBox.java:80)
    	at javax.swing.plaf.basic.BasicComboBoxUI.paintCurrentValue(BasicComboBoxUI.java:1214)
    	at com.sun.java.swing.plaf.windows.WindowsComboBoxUI.paintCurrentValue(WindowsComboBoxUI.java:311)
    	at javax.swing.plaf.basic.BasicComboBoxUI.paint(BasicComboBoxUI.java:905)
    	at com.sun.java.swing.plaf.windows.WindowsComboBoxUI.paint(WindowsComboBoxUI.java:217)
    	at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
    	at javax.swing.JComponent.paintComponent(JComponent.java:778)
    	at javax.swing.JComponent.paint(JComponent.java:1054)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paint(JComponent.java:1063)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paint(JComponent.java:1063)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paint(JComponent.java:1063)
    	at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
    	at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
    	at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
    	at javax.swing.JComponent.paint(JComponent.java:1040)
    	at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCall
    I am getting error on line shown bold in the first code. Help me to figure out this error.

    Thanks in advance.

  2. #2
    Join Date
    Oct 2011
    Posts
    23
    Rep Power
    0

    Default Re: Custom JComboBox

    It's on line No 81 in the first code block.

    Help me to figure out this.

    Thanks

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    arielb is offline Member
    Join Date
    Jan 2012
    Location
    Panamá
    Posts
    41
    Rep Power
    0

    Default Re: Custom JComboBox

    CustomComboBox.class.getResource search from classes directory of your project.
    For example:

    c:\jcomboboximage\build\classes\

    ultimately must be

    c:\jcomboboximage\build\classes\images\image.png

  5. #5
    Join Date
    Oct 2011
    Posts
    23
    Rep Power
    0

    Default Re: Custom JComboBox

    Hi arielb,

    I think CustomComboBox.class.getResource is going to right location as i printed two lines in the output .

    You can see that path is /C:/Documents%20and%20Settings/vns08103/My%20Documents/NetBeansProjects/JcomboBoxImage/build/classes/images/about.png

    & that's where the image is. But still it is giving error in ComboBoxRenderer class.

    Help me to figure out this.

    Thanks

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Custom JComboBox

    Hey -- did you read what I posted? You're not using a model nor adding to the combobox's default model, so of course it will be null.

  7. #7
    Join Date
    Oct 2011
    Posts
    23
    Rep Power
    0

    Default Re: Custom JComboBox

    Hello Fubarable,

    I read your reply and tries to add following line ..but could not understand what data should i pass. An string / ImageIcon array ??

    setModel(new javax.swing.DefaultComboBoxModel());

    Thanks .

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Custom JComboBox

    Best to experiment and try, and the combobox tutorials will show you how. You could try to a DefaultComboBoxModel object, fill it with your data and give it to your combo box object. Also, I don't see anything in your code that suggests that you should extend from JComboBox. Why not just use a combobox that has your custom renderer and is filled with your data rather than extend one?

Similar Threads

  1. Need help for a jcombobox
    By Alexis in forum AWT / Swing
    Replies: 4
    Last Post: 09-16-2011, 03:13 AM
  2. Replies: 0
    Last Post: 09-12-2011, 02:06 PM
  3. Need help with JCOMBOBOX
    By Slavon in forum AWT / Swing
    Replies: 1
    Last Post: 03-31-2011, 10:17 AM
  4. Replies: 2
    Last Post: 12-10-2009, 02:58 PM
  5. I need help with JComboBox
    By eva21 in forum New To Java
    Replies: 1
    Last Post: 11-28-2008, 11:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •