Я создал себе пользовательский JList
с JCheckBox
в качестве предметов.Все это работает довольно хорошо, но теперь я хочу иметь возможность выбрать CheckBox
только с клавиатуры.Также я не хочу использовать Mnemonics для каждого элемента CheckBox
.
Есть ли способ реализовать какой-то FocusListener
или что-то еще, чтобы я мог перемещаться с помощью клавиши табулятора?
Я пытался установить setFocusPainted(true)
и т. Д., Но у меня ничего не получалось.
Спасибо за ваше время и помощь.
Мой код:
public class JCheckBoxList extends JList<Object>{
private DefaultListModel<Object> model = null;
private JCheckBoxList selfPointer = null;
private boolean enabled = true;
@SuppressWarnings("unchecked")
/**
* Constructor.
*/
public JCheckBoxList() {
super();
model = new DefaultListModel<Object>();
selfPointer = this;
this.setModel(model);
this.setCellRenderer(new CheckBoxListCellRenderer());
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
int clicked_index = selfPointer.locationToIndex(evt.getPoint());
if (evt.getModifiers() == MouseEvent.BUTTON3_MASK) {
//right clicked
}else {
//left clicked
if (enabled) {
setSelected(clicked_index, !isSelected(clicked_index));
selfPointer.repaint(selfPointer.getCellBounds(clicked_index, clicked_index));
}
}
}
});
this.setVisibleRowCount(50);
this.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
/**
* Add new CheckBoxListItem to the JCheckBoxList.
* @param name Name of new Item.
*/
public void addElement(String name) {
model.addElement(new CheckBoxListItem(name));
}
/**
* Add new CheckBoxListItem to the JCheckBoxList.
* @param name Name of new Item.
* @param isSelected Boolean if new Item should be selected or not after creating.
*/
public void addElement(String name, boolean isSelected) {
CheckBoxListItem item = new CheckBoxListItem(name);
item.setSelected(isSelected);
model.addElement(item);
}
/**
* Get all selected Values from JCheckBoxList.
* @return ArrayList of type String with all selected values.
*/
public List<String> getSelectedValueList() {
List<String> returnList = new ArrayList<String>();
for(int i = 0; i < model.getSize(); i++) {
if (((CheckBoxListItem)model.getElementAt(i)).isSelected == true) {
returnList.add(model.getElementAt(i).toString());
}
}
if (returnList.isEmpty()){
return null;
}
return returnList;
}
/**
* Replaces Element at an specific index. Removes the old and creates a new one.
* @param index Integer index to identify object to replace.
* @param name Name of new item.
*/
public void replaceElementAt(int index, String name) {
model.removeElementAt(index);
model.insertElementAt(new CheckBoxListItem(name), index);;
}
/**
* Removes all Elements from JCheckBoxList.
*/
public void removeAll() {
model.removeAllElements();
}
/**
* Custom getElementAt method. Same functionality as List method.
* @param index Integer index of Element to get.
* @return Return String name of Element.
*/
public String getElementAt(int index) {
return model.getElementAt(index).toString();
}
/**
* Check if an Element is Selected.
* @param index Integer Index to identify Element.
* @return Returns whether Element is selected or not.
*/
public boolean isSelected(int index) {
return ((CheckBoxListItem)model.getElementAt(index)).isSelected;
}
/**
* Set the selected state of Element.
* @param index Integer Index of identify Element.
* @param isSelected Boolean value to set.
*/
public void setSelected(int index, boolean isSelected) {
((CheckBoxListItem)model.getElementAt(index)).setSelected(isSelected);
}
@Override
/*
* (non-Javadoc)
* @see javax.swing.JComponent#setEnabled(boolean)
*/
public void setEnabled(boolean arg0) {
enabled = arg0;
}
@Override
/*
* (non-Javadoc)
* @see java.awt.Component#isEnabled()
*/
public boolean isEnabled() {
return enabled;
}
/**
* Get all Values from JCheckBoxList as ArrayList.
* @return Returns ArrayList of type String with content of JCheckBoxList.
*/
public List<String> getValues() {
List<String> returnList = new ArrayList<String>();
for (int i = 0; i < model.getSize(); i++) {
returnList.add(model.getElementAt(i).toString());
}
return returnList;
}
private class CheckBoxListItem {
private String label;
private boolean isSelected = false;
private CheckBoxListItem(String label) {
this.label = label;
}
private boolean isSelected() {
return isSelected;
}
private void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
@Override
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return label;
}
}
@SuppressWarnings("rawtypes")
private class CheckBoxListCellRenderer extends JCheckBox implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
setComponentOrientation(list.getComponentOrientation());
setFont(list.getFont());
setBackground(list.getBackground());
setForeground(list.getForeground());
setSelected(((CheckBoxListItem) value).isSelected());
setEnabled(enabled);
setText(value == null ? "" : value.toString());
return this;
}
}
}