📄 checkboxlist.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* CheckBoxList.java
* Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
*/
package weka.gui;
import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
/**
* An extended JList that contains CheckBoxes. If necessary a CheckBoxListItem
* wrapper is added around the displayed object in any of the Model methods,
* e.g., addElement. For methods returning objects the opposite takes place,
* the wrapper is removed and only the payload object is returned.
*
* @author fracpete (fracpete at waikato dot ac dot nz)
* @version $Revision: 1.1 $
*/
public class CheckBoxList
extends JList {
/**
* represents an item in the CheckBoxListModel
*
* @author fracpete (fracpete at waikato dot ac dot nz)
* @version $Revision: 1.1 $
*/
protected class CheckBoxListItem {
/** whether item is checked or not */
private boolean m_Checked = false;
/** the actual object */
private Object m_Content = null;
/**
* initializes the item with the given object and initially unchecked
*
* @param o the content object
*/
public CheckBoxListItem(Object o) {
this(o, false);
}
/**
* initializes the item with the given object and whether it's checked
* initially
*
* @param o the content object
* @param checked whether the item should be checked initially
*/
public CheckBoxListItem(Object o, boolean checked) {
m_Checked = checked;
m_Content = o;
}
/**
* returns the content object
*/
public Object getContent() {
return m_Content;
}
/**
* sets the checked state of the item
*/
public void setChecked(boolean value) {
m_Checked = value;
}
/**
* returns the checked state of the item
*/
public boolean getChecked() {
return m_Checked;
}
/**
* returns the string representation of the content object
*/
public String toString() {
return m_Content.toString();
}
/**
* returns true if the "payload" objects of the current and the given
* CheckBoxListItem are the same.
*
* @param o the CheckBoxListItem to check
* @throws IllegalArgumentException if the provided object is not a CheckBoxListItem
*/
public boolean equals(Object o) {
if (!(o instanceof CheckBoxListItem))
throw new IllegalArgumentException("Must be a CheckBoxListItem!");
return getContent().equals(((CheckBoxListItem) o).getContent());
}
}
/**
* A specialized model.
*
* @author fracpete (fracpete at waikato dot ac dot nz)
* @version $Revision: 1.1 $
*/
public class CheckBoxListModel
extends DefaultListModel {
/**
* initializes the model with no data.
*/
public CheckBoxListModel() {
super();
}
/**
* Constructs a CheckBoxListModel from an array of objects and then applies
* setModel to it.
*
* @param listData the data to use
*/
public CheckBoxListModel(Object[] listData) {
for (int i = 0; i < listData.length; i++)
addElement(listData[i]);
}
/**
* Constructs a CheckBoxListModel from a Vector and then applies setModel
* to it.
*/
public CheckBoxListModel(Vector listData) {
for (int i = 0; i < listData.size(); i++)
addElement(listData.get(i));
}
/**
* Inserts the specified element at the specified position in this list.
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
*/
public void add(int index, Object element) {
if (!(element instanceof CheckBoxListItem))
super.add(index, new CheckBoxListItem(element));
else
super.add(index, element);
}
/**
* Adds the specified component to the end of this list.
*
* @param obj the component to be added
*/
public void addElement(Object obj) {
if (!(obj instanceof CheckBoxListItem))
super.addElement(new CheckBoxListItem(obj));
else
super.addElement(obj);
}
/**
* Tests whether the specified object is a component in this list.
*
* @param elem the element to check
* @return true if the element is in the list
*/
public boolean contains(Object elem) {
if (!(elem instanceof CheckBoxListItem))
return super.contains(new CheckBoxListItem(elem));
else
return super.contains(elem);
}
/**
* Copies the components of this list into the specified array.
*
* @param anArray the array into which the components get copied
* @throws IndexOutOfBoundsException if the array is not big enough
*/
public void copyInto(Object[] anArray) {
if (anArray.length < getSize())
throw new IndexOutOfBoundsException("Array not big enough!");
for (int i = 0; i < getSize(); i++)
anArray[i] = ((CheckBoxListItem) getElementAt(i)).getContent();
}
/**
* Returns the component at the specified index. Throws an
* ArrayIndexOutOfBoundsException if the index is negative or not less
* than the size of the list.
*
* @param index an index into this list
* @return the component at the specified index
* @throws ArrayIndexOutOfBoundsException
*/
public Object elementAt(int index) {
return ((CheckBoxListItem) super.elementAt(index)).getContent();
}
/**
* Returns the first component of this list. Throws a
* NoSuchElementException if this vector has no components.
*
* @return the first component of this list
* @throws NoSuchElementException
*/
public Object firstElement() {
return ((CheckBoxListItem) super.firstElement()).getContent();
}
/**
* Returns the element at the specified position in this list.
*
* @param index of element to return
* @throws ArrayIndexOutOfBoundsException
*/
public Object get(int index) {
return ((CheckBoxListItem) super.get(index)).getContent();
}
/**
* Returns the component at the specified index.
*
* @param index an index into this list
* @return the component at the specified index
* @throws ArrayIndexOutOfBoundsException
*/
public Object getElementAt(int index) {
return ((CheckBoxListItem) super.getElementAt(index)).getContent();
}
/**
* Searches for the first occurrence of elem.
*
* @param elem an object
* @return the index of the first occurrence of the argument in this list;
* returns -1 if the object is not found
*/
public int indexOf(Object elem) {
if (!(elem instanceof CheckBoxListItem))
return super.indexOf(new CheckBoxListItem(elem));
else
return super.indexOf(elem);
}
/**
* Searches for the first occurrence of elem, beginning the search at index.
*
* @param elem the desired component
* @param index the index from which to begin searching
* @return the index where the first occurrence of elem is found after index;
* returns -1 if the elem is not found in the list
*/
public int indexOf(Object elem, int index) {
if (!(elem instanceof CheckBoxListItem))
return super.indexOf(new CheckBoxListItem(elem), index);
else
return super.indexOf(elem, index);
}
/**
* Inserts the specified object as a component in this list at the
* specified index.
*
* @param obj the component to insert
* @param index where to insert the new component
* @throws ArrayIndexOutOfBoundsException
*/
public void insertElementAt(Object obj, int index) {
if (!(obj instanceof CheckBoxListItem))
super.insertElementAt(new CheckBoxListItem(obj), index);
else
super.insertElementAt(obj, index);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -