Oct 29

You need to add your JList (or any component) to a JScrollPane. Easiest way to do this is to pass your component to the JScrollPane’s constructor. You then add the JScrollPane to your component hierarchy (instead of adding your JList).


JScrollPane scrollPne = new JScrollPane(mylist);
panel.add(scrollPane);

written by objects \\ tags: ,

Oct 14

You can achieve that by creating a custom ListCellRenderer. Easiest is to subclass DefaultListCellRenderer and override the getListCellRendererComponent() method to add the appropriate icon.

That just leaves how to determine which icon to use for a given list item value. One solution to this is to provide the renderer with a set of mappings containing what icon to use for which value.


import java.awt.Component;
import java.util.HashMap;
import java.util.Map;

import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.plaf.basic.BasicIconFactory;
import javax.swing.plaf.metal.MetalIconFactory;

public class IconListRenderer
	extends DefaultListCellRenderer {

	private Map<Object, Icon> icons = null;

	public IconListRenderer(Map<Object, Icon> icons) {
		this.icons = icons;
	}

	@Override
	public Component getListCellRendererComponent(
		JList list, Object value, int index,
		boolean isSelected, boolean cellHasFocus) {

		// Get the renderer component from parent class

		JLabel label =
			(JLabel) super.getListCellRendererComponent(list,
				value, index, isSelected, cellHasFocus);

		// Get icon to use for the list item value

		Icon icon = icons.get(value);

		// Set icon to display for value

		label.setIcon(icon);
		return label;
	}

	public static void main(String[] args) {

		// setup mappings for which icon to use for each value

		Map<Object, Icon> icons = new HashMap<Object, Icon>();
		icons.put("details",
			MetalIconFactory.getFileChooserDetailViewIcon());
		icons.put("folder",
			MetalIconFactory.getTreeFolderIcon());
		icons.put("computer",
			MetalIconFactory.getTreeComputerIcon());

		JFrame frame = new JFrame("Icon List");
		frame.setDefaultCloseOperation(
			JFrame.DISPOSE_ON_CLOSE);

		// create a list with some test data

		JList list = new JList(
			new Object[] {
				"details", "computer", "folder", "computer"});

		// create a cell renderer to add the appropriate icon

		list.setCellRenderer(new IconListRenderer(icons));
		frame.add(list);
		frame.pack();
		frame.setVisible(true);
	}

}

written by objects \\ tags: , , ,

Sep 23

You need 3 things:

  • Make sure the items in your list have a property that indicates whether they are selected or not
  • Use a JCheckBox as a list cell renderer
  • A mouse listener to change the state of a list item when clicked

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;

public class CheckList
{
   public static void main(String args[])
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create a list containing CheckListItem's

      JList list = new JList(new CheckListItem[] {
            new CheckListItem("apple"),
            new CheckListItem("orange"),
            new CheckListItem("mango"),
            new CheckListItem("paw paw"),
            new CheckListItem("banana")});

      // Use a CheckListRenderer (see below)
      // to renderer list cells

      list.setCellRenderer(new CheckListRenderer());
      list.setSelectionMode(
         ListSelectionModel.SINGLE_SELECTION);

      // Add a mouse listener to handle changing selection

      list.addMouseListener(new MouseAdapter()
      {
         public void mouseClicked(MouseEvent event)
         {
            JList list = (JList) event.getSource();

            // Get index of item clicked

            int index = list.locationToIndex(event.getPoint());
            CheckListItem item = (CheckListItem)
               list.getModel().getElementAt(index);

            // Toggle selected state

            item.setSelected(! item.isSelected());

            // Repaint cell

            list.repaint(list.getCellBounds(index, index));
         }
      });   

      frame.getContentPane().add(new JScrollPane(list));
      frame.pack();
       frame.setVisible(true);
   }
}

// Represents items in the list that can be selected

class CheckListItem
{
   private String  label;
   private boolean isSelected = false;

   public CheckListItem(String label)
   {
      this.label = label;
   }

   public boolean isSelected()
   {
      return isSelected;
   }

   public void setSelected(boolean isSelected)
   {
      this.isSelected = isSelected;
   }

   public String toString()
   {
      return label;
   }
}

// Handles rendering cells in the list using a check box

class CheckListRenderer extends JCheckBox
   implements ListCellRenderer
{
   public Component getListCellRendererComponent(
         JList list, Object value, int index,
         boolean isSelected, boolean hasFocus)
   {
      setEnabled(list.isEnabled());
      setSelected(((CheckListItem)value).isSelected());
      setFont(list.getFont());
      setBackground(list.getBackground());
      setForeground(list.getForeground());
      setText(value.toString());
      return this;
   }
}

written by objects \\ tags: , ,