1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
class listRender implements ListCellRenderer{
private Boolean[] selection;
public listRender(int d){
selection=new Boolean[d];
for(int i=0;i<selection.length;i++){
selection[i]=false;
}
}
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
JCheckBox cbx=new JCheckBox(value.toString());
cbx.setSelected(selection[index]);
return cbx;
}
public void clickitem(int index){
selection[index]=!selection[index];
}
public Boolean isSelected(int index){
return selection[index];
}
}
class listListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent e) {
if(e.getValueIsAdjusting()){
return;
}
if(e.getSource().equals(lst)){
((listRender)lst.getCellRenderer()).clickitem(((JList)e.getSource()).getSelectedIndex());
lst.validate();
}
}
}
....
....
datalst=new String[3];
datalst[0]="item 1";
datalst[1]="item 2";
datalst[2]="item 3";
lst.setListData(datalst);
lst.setCellRenderer(new listRender(datalst.length));
lst.addListSelectionListener(new listListener());
lst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
Partager