/** * returns an array with the indices of all checked items * * @return the indices of all items that are currently checked */ public int[] getCheckedIndices() { Vector<Integer> list; int[] result; int i; // traverse over model list = new Vector<Integer>(); 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] = list.get(i).intValue(); } return result; } }
/** * returns an array with the indices of all checked items * * @return the indices of all items that are currently checked */ public int[] getCheckedIndices() { Vector<Integer> list; int[] result; int i; // traverse over model list = new Vector<Integer>(); 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] = list.get(i).intValue(); } return result; } }
@Override public void mousePressed(MouseEvent e) { int index = locationToIndex(e.getPoint()); if (index != -1) { setChecked(index, !getChecked(index)); repaint(); } } });
@Override public void keyTyped(KeyEvent e) { if ((e.getKeyChar() == ' ') && (e.getModifiers() == 0)) { int index = getSelectedIndex(); setChecked(index, !getChecked(index)); e.consume(); repaint(); } } });
@Override public void mousePressed(MouseEvent e) { int index = locationToIndex(e.getPoint()); if (index != -1) { setChecked(index, !getChecked(index)); repaint(); } } });
@Override public void keyTyped(KeyEvent e) { if ((e.getKeyChar() == ' ') && (e.getModifiers() == 0)) { int index = getSelectedIndex(); setChecked(index, !getChecked(index)); e.consume(); repaint(); } } });
/** * 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. */ @Override 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; } }
/** * 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. */ @Override 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; } }