KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones
java,jtable,jtree,swing Return to JAVA JTree list

Best books about JAVA programming:

JAVA JList Checkbox icn

The following code add checkbox and icons to JList. This example has three files:, and

/*Example modified from

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.metal.*;

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);





Sponsored link
Java JTabbedPane
JTabbedPane Preview List
Java JComboBox
JComboBox Preview List
Java JTooltip
JTooltip Preview List
Java JList
JList Preview List
Java JTree
JTree Preview List 1
JTree Preview List 2
JTable Preview List 1
JTable Preview List 2
JTable Preview List 3
JTable Preview List 4
JTable Preview List 5
JTable Preview List 6
JTable Preview List 7
JAVA Jslide
JSlide Preview List



Best books about JAVA programming from Amazon
Professional Java, JDK 5 Edition (Paperback Simply Java Programming: An Application-Driven Tutorial Approach (Paperback) Effective Java Programming Language Guide (Paperback) Head First Java, 2nd Edition (Paperback) Java : An Introduction to Problem Solving and Programming (4th Edition)


Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © All rights reserved(new). Privacy|Copyright
designed by

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer