setModel(model); setCellRenderer(new CheckBoxListRenderer()); addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) {
@Override public void keyTyped(KeyEvent e) { if ((e.getKeyChar() == ' ') && (e.getModifiers() == 0)) { int index = getSelectedIndex(); setChecked(index, !getChecked(index)); e.consume(); repaint(); } } });
/** * 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); }
@Override public void mousePressed(MouseEvent e) { int index = locationToIndex(e.getPoint()); if (index != -1) { setChecked(index, !getChecked(index)); repaint(); } } });
/** * 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; } }
/** * Constructs a CheckBoxListModel from an array of objects and then applies * setModel to it. * * @param listData the data to use */ @Override public void setListData(Object[] listData) { setModel(new CheckBoxListModel(listData)); }
/** * 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; } }
@Override public void mousePressed(MouseEvent e) { int index = locationToIndex(e.getPoint()); if (index != -1) { setChecked(index, !getChecked(index)); repaint(); } } });
/** * 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; } }
/** * Constructs a CheckBoxListModel from a Vector and then applies setModel to * it. */ @Override public void setListData(@SuppressWarnings("rawtypes") Vector listData) { setModel(new CheckBoxListModel(listData)); }
/** * 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; } }
@Override public void keyTyped(KeyEvent e) { if ((e.getKeyChar() == ' ') && (e.getModifiers() == 0)) { int index = getSelectedIndex(); setChecked(index, !getChecked(index)); e.consume(); repaint(); } } });
setModel(model); setCellRenderer(new CheckBoxListRenderer()); addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) {
/** * 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); }
/** * Constructs a CheckBoxListModel from a Vector and then applies setModel to * it. */ @Override public void setListData(@SuppressWarnings("rawtypes") Vector listData) { setModel(new CheckBoxListModel(listData)); }
/** * 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); }
/** * Constructs a CheckBoxListModel from an array of objects and then applies * setModel to it. * * @param listData the data to use */ @Override public void setListData(Object[] 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); }
model = (CheckBoxListModel) m_List.getModel(); for (Capability cap : Capability.values()) { model.addElement(cap);
model = (CheckBoxListModel) m_List.getModel(); for (Capability cap : Capability.values()) { model.addElement(cap);