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());Here is where I set the image Icon infoCode: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 the getImgName() methodCode: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 where I setListDataCode: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; }
Code:musicListBox.setListData(getPanel().toArray());Any idea why I am not seeing the image?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; }


Reply With Quote