当前位置: 首页 > 工具软件 > jCheckboxList > 使用案例 >

java checkboxlist_Java通过定制的JCHECKBOXLIST使用制表符键导航

匡凌
2023-12-01

我为自己建立了一个习惯

JList

具有

JCheckBox

作为物品。一切都很好,但现在我想能够选择

CheckBox

只有键盘输入。另外,我不想给每个人都用助记符

复选框

项目。

有什么方法可以实现

FocusListener

或者是一些我可以用制表键导航的东西?

我试着设置

setFocusPainted(true)

等等,但对我来说没什么用。

谢谢你的时间和帮助。

我的代码:

public class JCheckBoxList extends JList{

private DefaultListModel model = null;

private JCheckBoxList selfPointer = null;

private boolean enabled = true;

@SuppressWarnings("unchecked")

/**

* Constructor.

*/

public JCheckBoxList() {

super();

model = new DefaultListModel();

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 getSelectedValueList() {

List returnList = new ArrayList();

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 getValues() {

List returnList = new ArrayList();

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;

}

}

}

 类似资料: