你需要实现ListCellRenderer加入checkbox选择框。从网上COPY了一份代码,你可以参考下:JList with CheckBoxesWe wrap the given ListCellRenderer to add CheckBox. This allows you to reuse existing renderers.// @author Santhosh Kumar T - [email protected] public class CheckListCellRenderer extends JPanel implements ListCellRenderer{ private ListCellRenderer delegate; private ListSelectionModel selectionModel; private JCheckBox checkBox = new JCheckBox();
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){ Component renderer = delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); checkBox.setSelected(selectionModel.isSelectedIndex(index)); removeAll(); add(checkBox, BorderLayout.WEST); add(renderer, BorderLayout.CENTER); return this; } } We use DefaultListCellRenderer to store checkSelection. This allows us to listen for changes in check selection very easily.Now here is the controller class which is responsible of interpreting mouse events.// @author Santhosh Kumar T - [email protected] public class CheckListManager extends MouseAdapter implements ListSelectionListener, ActionListener{ private ListSelectionModel selectionModel = new DefaultListSelectionModel(); private JList list = new JList(); int hotspot = new JCheckBox().getPreferredSize().width;
public void mouseClicked(MouseEvent me){ int index = list.locationToIndex(me.getPoint()); if(index<0) return; if(me.getX()>list.getCellBounds(index, index).x+hotspot) return; toggleSelection(index); }
public void actionPerformed(ActionEvent e){ toggleSelection(list.getSelectedIndex()); } } implements MouseListener to trap mouse click inside checkbox and toggle selection implements ListSelectionListener to update JList when check selection gets changes implements ActionListener to toggle check selection of selected item when SPACE is pressed. How to use this: // make your JList as check list CheckListManager checkListManager = new CheckListManager(yourList);
// to get checked items checkListManager.getSelectionModel().isSelectedIndex(index); So your can convert JList with any model and renderer to CheckList with just one line.
public class CheckListCellRenderer extends JPanel implements ListCellRenderer{
private ListCellRenderer delegate;
private ListSelectionModel selectionModel;
private JCheckBox checkBox = new JCheckBox();
public CheckListCellRenderer(ListCellRenderer renderer, ListSelectionModel selectionModel){
this.delegate = renderer;
this.selectionModel = selectionModel;
setLayout(new BorderLayout());
setOpaque(false);
checkBox.setOpaque(false);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){
Component renderer = delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
checkBox.setSelected(selectionModel.isSelectedIndex(index));
removeAll();
add(checkBox, BorderLayout.WEST);
add(renderer, BorderLayout.CENTER);
return this;
}
}
We use DefaultListCellRenderer to store checkSelection. This allows us to listen for changes in check selection very easily.Now here is the controller class which is responsible of interpreting mouse events.// @author Santhosh Kumar T - [email protected]
public class CheckListManager extends MouseAdapter implements ListSelectionListener, ActionListener{
private ListSelectionModel selectionModel = new DefaultListSelectionModel();
private JList list = new JList();
int hotspot = new JCheckBox().getPreferredSize().width;
public CheckListManager(JList list){
this.list = list;
list.setCellRenderer(new CheckListCellRenderer(list.getCellRenderer(), selectionModel));
list.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), JComponent.WHEN_FOCUSED);
list.addMouseListener(this);
selectionModel.addListSelectionListener(this);
}
public ListSelectionModel getSelectionModel(){
return selectionModel;
}
private void toggleSelection(int index){
if(index<0)
return;
if(selectionModel.isSelectedIndex(index))
selectionModel.removeSelectionInterval(index, index);
else
selectionModel.addSelectionInterval(index, index);
}
/*------------------------------[ MouseListener ]-------------------------------------*/
public void mouseClicked(MouseEvent me){
int index = list.locationToIndex(me.getPoint());
if(index<0)
return;
if(me.getX()>list.getCellBounds(index, index).x+hotspot)
return;
toggleSelection(index);
}
/*-----------------------------[ ListSelectionListener ]---------------------------------*/
public void valueChanged(ListSelectionEvent e){
list.repaint(list.getCellBounds(e.getFirstIndex(), e.getLastIndex()));
}
/*-----------------------------[ ActionListener ]------------------------------*/
public void actionPerformed(ActionEvent e){
toggleSelection(list.getSelectedIndex());
}
}
implements MouseListener to trap mouse click inside checkbox and toggle selection
implements ListSelectionListener to update JList when check selection gets changes
implements ActionListener to toggle check selection of selected item when SPACE is pressed.
How to use this: // make your JList as check list
CheckListManager checkListManager = new CheckListManager(yourList);
// to get checked items
checkListManager.getSelectionModel().isSelectedIndex(index);
So your can convert JList with any model and renderer to CheckList with just one line.