- public class CheckBoxJList extends JList
- implements ListSelectionListener {
- static Color listForeground, listBackground;
- static {
- UIDefaults uid = UIManager.getLookAndFeel().getDefaults();
- listForeground =uid.getColor ("List.foreground");
- listBackground =uid.getColor ("List.background");
- }
- HashSet selectionCache = new HashSet();
- int toggleIndex = -1;
- boolean toggleWasSelected;
- public CheckBoxJList() {
- super();
- setCellRenderer (new CheckBoxListCellRenderer());
- addListSelectionListener (this);
- }
- public void valueChanged (ListSelectionEvent lse) {
- if (! lse.getValueIsAdjusting()) {
- removeListSelectionListener (this);
- // determine if this selection has added or removed items
- HashSet newSelections = new HashSet();
- int size = getModel().getSize();
- for (int i=0; i<size; i++) {
- if (getSelectionModel().isSelectedIndex(i)) {
- newSelections.add (new Integer(i));
- }
- }
- // turn on everything that was selected previously
- Iterator it = selectionCache.iterator();
- while (it.hasNext()) {
- int index = ((Integer) it.next()).intValue();
- getSelectionModel().addSelectionInterval(index, index);
- }
- // add or remove the delta
- it = newSelections.iterator();
- while (it.hasNext()) {
- Integer nextInt = (Integer) it.next();
- int index = nextInt.intValue();
- if (selectionCache.contains (nextInt))
- getSelectionModel().removeSelectionInterval (index, index);
- else
- getSelectionModel().addSelectionInterval (index, index);
- }
- // save selections for next time
- selectionCache.clear();
- for (int i=0; i<size; i++) {
- if (getSelectionModel().isSelectedIndex(i)) {
- selectionCache.add (new Integer(i));
- }
- }
- addListSelectionListener (this);
- }
- }
- class CheckBoxListCellRenderer extends JComponent
- implements ListCellRenderer {
- DefaultListCellRenderer defaultComp;
- JCheckBox checkbox;
- public CheckBoxListCellRenderer() {
- setLayout (new BorderLayout());
- defaultComp = new DefaultListCellRenderer();
- checkbox = new JCheckBox();
- add (checkbox, BorderLayout.WEST);
- add (defaultComp, BorderLayout.CENTER);
- }
- public Component getListCellRendererComponent(JList list,
- Object value,
- int index,
- boolean isSelected,
- boolean cellHasFocus){
- defaultComp.getListCellRendererComponent (list, value, index,
- isSelected, cellHasFocus);
- checkbox.setSelected (isSelected);
- Component[] comps = getComponents();
- for (int i=0; i<comps.length; i++) {
- comps[i].setForeground (listForeground);
- comps[i].setBackground (listBackground);
- }
- return this;
- }
- }
- public static void main (String[] args) {
- JList list = new CheckBoxJList ();
- DefaultListModel defModel = new DefaultListModel();
- list.setModel (defModel);
- String[] listItems = {
- "Chris", "Joshua", "Daniel", "Michael",
- "Don", "Kimi", "Kelly", "Keagan"
- };
- Iterator it = Arrays.asList(listItems).iterator();
- while (it.hasNext())
- defModel.addElement (it.next());
- // show list
- JScrollPane scroller =
- new JScrollPane (list,
- ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
- ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
- JFrame frame = new JFrame ("Checkbox JList");
- frame.getContentPane().add (scroller);
- frame.pack();
- frame.setVisible(true);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/300820135509.html
来源: http://www.codesnippet.cn/detail/300820135509.html