Have images in a JListTag(s): Swing


This snippet uses these 2 images   
import javax.swing.*;
import java.awt.*;
import java.util.*;

public class JListWithImages extends JList {

 public JListWithImages() { 
   setCellRenderer(new CustomCellRenderer()); 
   }

 public static void main(String[] args) {
   JFrame frame = new JFrame();
   JPanel panel = new JPanel();
   Vector vector = new Vector();
   panel.setForeground(Color.black);
   panel.setBackground(Color.white);

   // first line
   JPanel jp1 = new JPanel();
   jp1.add(new JLabel(new ImageIcon("gumby.gif")));
   jp1.add(new JLabel("A line for Gumby"));
   jp1.add(new JLabel(new ImageIcon("gumby2.gif")));

   // second line
   JPanel jp2 = new JPanel();
   jp2.add(new JLabel(new ImageIcon("gumby.gif")));
   jp2.add(new JLabel("Another line for Gumby"));
   jp2.add(new JLabel(new ImageIcon("gumby2.gif")));

   vector.addElement(jp1);
   vector.addElement(jp2);

   JListWithImages jlwi = new JListWithImages();
   jlwi.setListData(vector);

   panel.add(jlwi);
   frame.getContentPane().add(panel);
   frame.setSize(300,300);
   frame.setVisible(true);
   }

 class CustomCellRenderer implements ListCellRenderer {
   public Component getListCellRendererComponent
    (JList list, Object value, int index, 
     boolean isSelected,boolean cellHasFocus) {
     Component component = (Component)value;
     component.setBackground
      (isSelected ? Color.black : Color.white);
     component.setForeground
      (isSelected ? Color.white : Color.black);
     return component;
     }
   }
}

The above How-to use the default layout, so each line line in the JList are centered. The version below is showing each line left justified instead.

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.util.*;

public class JListWithImages extends JList {

 public JListWithImages() {
   setCellRenderer(new CustomCellRenderer());
   }

 public static void main(String[] args) {
   JFrame frame = new JFrame();
   JPanel panel = new JPanel();
   Vector vector = new Vector();
   panel.setForeground(Color.black);
   panel.setBackground(Color.white);

   // first line
   JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.LEFT));   // NEW
   jp1.add(new JLabel(new ImageIcon("gumby.gif")));
   jp1.add(new JLabel("A line for Gumby"));
   jp1.add(new JLabel(new ImageIcon("gumby2.gif")));

   // second line
   JPanel jp2 = new JPanel(new FlowLayout(FlowLayout.LEFT));  // NEW
   jp2.add(new JLabel(new ImageIcon("gumby.gif")));
   jp2.add(new JLabel("Another line for Gumby"));
   jp2.add(new JLabel(new ImageIcon("gumby2.gif")));

   vector.addElement(jp1);
   vector.addElement(jp2);

   JListWithImages jlwi = new JListWithImages();
   jlwi.setListData(vector);
   jlwi.setBorder(new LineBorder(Color.black));

   panel.add(jlwi);
   frame.getContentPane().add(panel);
   frame.setSize(300,300);
   frame.setVisible(true);
   }

 class CustomCellRenderer implements ListCellRenderer {
   public Component getListCellRendererComponent
    (JList list, Object value, int index,
     boolean isSelected,boolean cellHasFocus) {
     Component component = (Component)value;
     component.setBackground
      (isSelected ? Color.black : Color.white);
     component.setForeground
      (isSelected ? Color.white : Color.black);
     return component;
     }
   }
}

blog comments powered by Disqus