我尝试做的是JList和复选框。我无法选择多个项目。当我这样做时,已经选中的项将被取消选中。我错过了什么?
JList recipientsList = new JList(
new RecipientWrapper[] { new RecipientWrapper("apple"), new RecipientWrapper("orange"),
new RecipientWrapper("mango"), new RecipientWrapper("paw paw"), new RecipientWrapper("banana") });
recipientsList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
recipientsList.setCellRenderer(new CheckboxListRenderer());
class CheckboxListRenderer implements ListCellRenderer<RecipientWrapper>
{
private static final long serialVersionUID = 1L;
private JCheckBox checkBox;
@Override
public Component getListCellRendererComponent(JList list,RecipientWrapper value, int index, boolean isSelected,
boolean cellHasFocus)
{
Component c = null;
if(value != null)
{
getCheckBox().setText(value.toString());
checkBox.setSelected(isSelected);
value.setSelected(isSelected);
c = checkBox;
}
else
{
c = new JLabel();
}
return c;
}
private JCheckBox getCheckBox()
{
if(checkBox == null)
{
checkBox = new JCheckBox();
}
return checkBox;
}
}发布于 2017-05-19 12:52:53
这就是JList的预期行为。您需要按住、Ctrl、或Shift键来选择列表中的多个值。
如果要更改此行为,可能需要考虑对复选框组件使用ActionListener并跨事件持久化。
https://stackoverflow.com/questions/44070477
复制相似问题