📄 checkboxlist.java
字号:
/**
* Returns the last component of the list. Throws a NoSuchElementException
* if this vector has no components.
*
* @return the last component of the list
* @throws NoSuchElementException
*/
public Object lastElement() {
return ((CheckBoxListItem) lastElement()).getContent();
}
/**
* Returns the index of the last occurrence of elem.
*
* @param elem the desired component
* @return the index of the last occurrence of elem in the list;
* returns -1 if the object is not found
*/
public int lastIndexOf(Object elem) {
if (!(elem instanceof CheckBoxListItem))
return super.lastIndexOf(new CheckBoxListItem(elem));
else
return super.lastIndexOf(elem);
}
/**
* Searches backwards for elem, starting from the specified index,
* and returns an index to it.
*
* @param elem the desired component
* @param index the index to start searching from
* @return the index of the last occurrence of the elem in this
* list at position less than index; returns -1 if the
* object is not found
*/
public int lastIndexOf(Object elem, int index) {
if (!(elem instanceof CheckBoxListItem))
return super.lastIndexOf(new CheckBoxListItem(elem), index);
else
return super.lastIndexOf(elem, index);
}
/**
* Removes the element at the specified position in this list. Returns the
* element that was removed from the list.
*
* @param index the index of the element to removed
* @throws ArrayIndexOutOfBoundsException
*/
public Object remove(int index) {
return ((CheckBoxListItem) remove(index)).getContent();
}
/**
* Removes the first (lowest-indexed) occurrence of the argument from this
* list.
*
* @param obj the component to be removed
* @return true if the argument was a component of this list;
* false otherwise
*/
public boolean removeElement(Object obj) {
if (!(obj instanceof CheckBoxListItem))
return super.removeElement(new CheckBoxListItem(obj));
else
return super.removeElement(obj);
}
/**
* Replaces the element at the specified position in this list with the
* specified element.
*
* @param index index of element to replace
* @param element element to be stored at the specified position
* @throws ArrayIndexOutOfBoundsException
*/
public Object set(int index, Object element) {
if (!(element instanceof CheckBoxListItem))
return ((CheckBoxListItem) super.set(index, new CheckBoxListItem(element))).getContent();
else
return ((CheckBoxListItem) super.set(index, element)).getContent();
}
/**
* Sets the component at the specified index of this list to be the
* specified object. The previous component at that position is discarded.
*
* @param obj what the component is to be set to
* @param index the specified index
* @throws ArrayIndexOutOfBoundsException
*/
public void setElementAt(Object obj, int index) {
if (!(obj instanceof CheckBoxListItem))
super.setElementAt(new CheckBoxListItem(obj), index);
else
super.setElementAt(obj, index);
}
/**
* Returns an array containing all of the elements in this list in the
* correct order.
*
* @return an array containing the elements of the list
*/
public Object[] toArray() {
Object[] result;
Object[] internal;
int i;
internal = super.toArray();
result = new Object[internal.length];
for (i = 0; i < internal.length; i++)
result[i] = ((CheckBoxListItem) internal[i]).getContent();
return result;
}
/**
* returns the checked state of the element at the given index
*
* @param index the index of the element to return the checked state for
* @return the checked state of the specifed element
*/
public boolean getChecked(int index) {
return ((CheckBoxListItem) super.getElementAt(index)).getChecked();
}
/**
* sets the checked state of the element at the given index
*
* @param index the index of the element to set the checked state for
* @param checked the new checked state
*/
public void setChecked(int index, boolean checked) {
((CheckBoxListItem) super.getElementAt(index)).setChecked(checked);
}
}
/**
* A specialized CellRenderer for the CheckBoxList
*
* @author fracpete (fracpete at waikato dot ac dot nz)
* @version $Revision: 1.1 $
* @see CheckBoxList
*/
public class CheckBoxListRenderer
extends JCheckBox
implements ListCellRenderer {
/**
* Return a component that has been configured to display the specified
* value.
*
* @param list The JList we're painting.
* @param value The value returned by list.getModel().getElementAt(index).
* @param index The cells index.
* @param isSelected True if the specified cell was selected.
* @param cellHasFocus True if the specified cell has the focus.
* @return A component whose paint() method will render the
* specified value.
*/
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
setText(value.toString());
setSelected(((CheckBoxList) list).getChecked(index));
setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
setFocusPainted(false);
return this;
}
}
/**
* initializes the list with an empty CheckBoxListModel
*/
public CheckBoxList() {
this(null);
}
/**
* initializes the list with the given CheckBoxListModel
*
* @param model the model to initialize with
*/
public CheckBoxList(CheckBoxListModel model) {
super();
if (model == null)
model = this.new CheckBoxListModel();
setModel(model);
setCellRenderer(new CheckBoxListRenderer());
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int index = locationToIndex(e.getPoint());
if (index != -1) {
setChecked(index, !getChecked(index));
repaint();
}
}
});
addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if ( (e.getKeyChar() == ' ') && (e.getModifiers() == 0) ) {
int index = getSelectedIndex();
setChecked(index, !getChecked(index));
e.consume();
repaint();
}
}
});
}
/**
* sets the model - must be an instance of CheckBoxListModel
*
* @param model the model to use
* @throws IllegalArgumentException if the model is not an instance of
* CheckBoxListModel
* @see CheckBoxListModel
*/
public void setModel(ListModel model) {
if (!(model instanceof CheckBoxListModel))
throw new IllegalArgumentException("Model must be an instance of CheckBoxListModel!");
super.setModel(model);
}
/**
* Constructs a CheckBoxListModel from an array of objects and then applies
* setModel to it.
*
* @param listData the data to use
*/
public void setListData(Object[] listData) {
setModel(new CheckBoxListModel(listData));
}
/**
* Constructs a CheckBoxListModel from a Vector and then applies setModel
* to it.
*/
public void setListData(Vector listData) {
setModel(new CheckBoxListModel(listData));
}
/**
* returns the checked state of the element at the given index
*
* @param index the index of the element to return the checked state for
* @return the checked state of the specifed element
*/
public boolean getChecked(int index) {
return ((CheckBoxListModel) getModel()).getChecked(index);
}
/**
* sets the checked state of the element at the given index
*
* @param index the index of the element to set the checked state for
* @param checked the new checked state
*/
public void setChecked(int index, boolean checked) {
((CheckBoxListModel) getModel()).setChecked(index, checked);
}
/**
* returns an array with the indices of all checked items
*
* @return the indices of all items that are currently checked
*/
public int[] getCheckedIndices() {
Vector list;
int[] result;
int i;
// traverse over model
list = new Vector();
for (i = 0; i < getModel().getSize(); i++) {
if (getChecked(i))
list.add(new Integer(i));
}
// generate result array
result = new int[list.size()];
for (i = 0; i < list.size(); i++) {
result[i] = ((Integer) list.get(i)).intValue();
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -