Add JLabel with image to JList to show all the images

后端 未结 2 1465
南笙
南笙 2020-11-27 08:27

Here is my code. It does not show images in the frame and instead shows some text. would anybody please suggest me that what change I should make in the code so that it all

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 09:06

    you can use listcellrenderer to display both image and text in jlist probably like the one below for showing label with icon in list

     public class myRenderer extends DefaultListCellRenderer
    {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index,boolean isSelected, boolean cellHasFocus) 
        {
            //JLabel l = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(value instanceof JLabel)
            {
                this.setText(((JLabel)value).getText());
                this.setIcon(((JLabel)value).getIcon());
            }
            return this;
        }
    }
    

提交回复
热议问题