devdaily home | java | perl | unix | directory | blog

What this is

This file is part of the Tame Swing collection that we have compiled. All of the Java source code in these examples was created by Nobuo Tamemasa. As these examples have become hard to find on the internet, we have put them in one place here, supported by minimal advertising. In time we will include images of each running sample Java/Swing application.

The intent of having these files here is to help you learn Java and Swing by example, and Tame certainly created some terrific examples of the power of the Swing framework.

Other links


java, swing, example, sample, source, custom, list, checklist, event, listener, ListModel

The source code

/* (swing1.1.1beta2) */
package tame.examples;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.metal.MetalIconFactory;

import tame.tree.CheckRenderer;

@author Nobuo Tamemasa
@version 1.0 04/24/99
public class CheckListExample extends JFrame {

  public CheckListExample() {
    super("CheckList Example");
    String[] strs = {"swing", "home", "basic", "metal", "JList"};

    final JList list = new JList( createData(strs) );

    // set "home" icon
    Icon icon = MetalIconFactory.getFileChooserHomeFolderIcon();

    list.setCellRenderer(new CheckListRenderer());
    list.setBorder(new EmptyBorder(0,4,0,0));
    list.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        int index = list.locationToIndex(e.getPoint());
        CheckableItem item = (CheckableItem)list.getModel().getElementAt(index);
        item.setSelected(! item.isSelected());
        Rectangle rect = list.getCellBounds(index, index);
    JScrollPane sp = new JScrollPane(list);

    final JTextArea textArea = new JTextArea(3,10);
    JScrollPane textPanel = new JScrollPane(textArea);
    JButton printButton = new JButton("print");
    printButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        ListModel model = list.getModel();
        int n = model.getSize();
        for (int i=0;i<n;i++) {
          CheckableItem item = (CheckableItem)model.getElementAt(i);
          if (item.isSelected()) {
    JButton clearButton = new JButton("clear");
    clearButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
    JPanel panel = new JPanel(new GridLayout(2,1));

    getContentPane().add(sp,    BorderLayout.CENTER);
    getContentPane().add(panel, BorderLayout.EAST);
    getContentPane().add(textPanel, BorderLayout.SOUTH);

  private CheckableItem[] createData(String[] strs) {
    int n = strs.length;
    CheckableItem[] items = new CheckableItem[n];
    for (int i=0;i<n;i++) {
      items[i] = new CheckableItem(strs[i]);
    return items;

  class CheckableItem {
    private String  str;
    private boolean isSelected;
    private Icon icon;

    public CheckableItem(String str) {
      this.str = str;
      isSelected = false;

    public void setSelected(boolean b) {
      isSelected = b;

    public boolean isSelected() {
      return isSelected;

    public String toString() {
      return str;

    public void setIcon(Icon icon) {
      this.icon = icon;

    public Icon getIcon() {
      return icon;

  class CheckListRenderer extends CheckRenderer implements ListCellRenderer {
    Icon commonIcon;

    public CheckListRenderer() {
      commonIcon = UIManager.getIcon("Tree.leafIcon");

    public Component getListCellRendererComponent(JList list, Object value,
               int index, boolean isSelected, boolean hasFocus) {
      Icon icon = ((CheckableItem)value).getIcon();
      if (icon == null) {
        icon = commonIcon;
      return this;

  public static void main(String args[]) {
    CheckListExample frame = new CheckListExample();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {System.exit(0);}
    frame.setSize(300, 200);

Copyright 1998-2009
All Rights Reserved.