CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2006
    Location
    Wantagh,NY
    Posts
    151

    Help adding ImageIcon to JList.

    I have a jlist that uses an ImageListCellRenderer. I create an icon and pass it an image. Then I attempt to do setListData. Everything compiles fine but no image is displayed.


    Here is where I set the ImageListCellRenderer()

    Code:
    musicListBox.setCellRenderer(new ImageListCellRenderer());
    Code:
    class ImageListCellRenderer implements ListCellRenderer
    {
    	  
    	   
    	  public Component getListCellRendererComponent(JList jlist, 
    	                                                Object value, 
    	                                                int cellIndex, 
    	                                                boolean isSelected, 
    	                                                boolean cellHasFocus)
    	  {
    		  
    	    if (value instanceof JPanel)
    	    {
    	      Component component = (Component) value;
    	      component.setForeground (Color.white);
    	      component.setBackground (Color.white);
    	      return component;
    	    }
    	    else
    	    {
    	    	return new JLabel("???");
    	    }
    	  }
    }
    Here is where I set the image Icon info

    Code:
    logoIcon = (new ImageIcon("C:\\Users\\mfierro\\Desktop\\crs471\\SourceCode\\Solution - ex71\\images\\" + getImgName()));
    		System.out.println(logoIcon);
    		logoLabel = new JLabel(logoIcon,JLabel.CENTER) ;
    		logoPanel = new JPanel();
    		logoPanel.add(logoLabel);
    Here is the getImgName() method
    Code:
    public String getImgName(){
    		
            String imgName = "";
    
             if(MainFrame.sel == 0){
    	imgName = "snes.jpg";
                    return imgName;
             }
            if(MainFrame.sel == 1){
                     imgName = "nes.jpg";
                     return imgName; 
            }
         	
            else{
         	return imgName;
            }
    Here is where I setListData
    Code:
    musicListBox.setListData(getPanel().toArray());
    Code:
    Here is the getPanel() method 
    public ArrayList<Object>getPanel(){
    		
               ArrayList<Object> obj = new ArrayList<Object>();
    	
    	
               Object item = (String) getImgName();
               obj.add(item);
               return obj;
    }
    Any idea why I am not seeing the image?

  2. #2
    Join Date
    Sep 2006
    Location
    Wantagh,NY
    Posts
    151

    Re: Help adding ImageIcon to JList.

    Sorry, never mind. Caught my mistake. The getPanel() method should have been this.


    Code:
    public ArrayList<Object>getPanel(){
    		
    		ArrayList<Object> obj = new ArrayList<Object>();
    	 
    		Object item = logoPanel;
    		 obj.add(item);
    	    return obj;
    	}
    I think I need to rest my eyes for a while.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured