您好我无法切换JList中的复选框,我希望当点击一个项目时勾选复选框,并且如果它被勾选再次我想它切换到unticked。我希望可以在不使用Ctrl或Shift键的情况下勾选或取消勾选多个项目。JCheckBox检查在JList中的切换逻辑的问题
public class CustCellRenderer extends JCheckBox
implements ListCellRenderer
{
boolean selected = false;
void CustCellRenderer()
{
setOpaque(true);
setIconTextGap(12);
}
// allows a custom list cell rendering which will enable me to display an icon as well as filename
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
JCheckBox checkBox = (JCheckBox)value;
if (isSelected)
{
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
if (!selected)
{
selected = true;
setSelected(selected);
}
else
{
selected = false;
setSelected(selected);
}
}
else
{
setBackground(list.getBackground());
setForeground(list.getForeground());
setSelected(selected);
}
setText(checkBox.getText());
return this;
}
}
这是我如何试图向表中添加数据,因为某些原因没有出现任何想法?
public void addDirectoryLabelsToList()
{
// clears all the previous labels from the listModel to ensure only labels
// that refelect the current directory are shown
for (int x = 0; x < tableModel.getRowCount(); x++)
tableModel.removeRow(x);
// iterate through the dirLabels and add them to the listModel
for (JCheckBox j : dirLabels)
{
Vector<Object> obj = new Vector<>();
obj.add(Boolean.FALSE);
obj.add(j.getText());
tableModel.addRow(obj);
}
}
所以它不能正确使用的JList做些什么呢? –
这一努力将是相当可观的。 – trashgod
JTable似乎也会涉及很多工作:( –