简体   繁体   中英

Selecting all items in a list

I'm working on Java Swing List and I wanna select all its values or items with the use of jCheckBox . I tried searching some answer on google and I could hardly find any.

I hope I can get answers from here.

Thanks.

EDIT:

In my swing list, I have these items for example:

Item 1
Item 2
Item 3

and I have a checkbox label as ' Select All '.

So what I want is when my checkbox is selected/checked, All the items in the swing list will be selected at once.

You want JList's

" void setSelectedIndices(int[] indices) Changes the selection to be the set of indices specified by the given array."

method. Don't forget to activate the property that allows multiselect Also remember to cancel stuff when the box is unchecked!

good luck

Create a setter and getter for list size. The getter will be the end point of setSelectionInterval(start, end);

JList list = new JList();
private void insertItem(){
DefaultListModel<String> list_model = new DefaultListModel<String>();
String listData[] = {"Fish", "Pork", "Chicken", "Curry"};
for(int i = 0; i < listData.length; i++)
list_model.addElement(listData[i]);
list.setModel(list_model); setListSize(list.size());
}

foodCb.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent evt) {
if(evt.getStateChange() == ItemEvent.SELECTED)
list.setSelectionInterval(0, getListSize());
else
list.clearSelection();
}
});

int list_size = 0; private void setListSize(int size) { list_size = size; } { list_size = size; }

private void getListSize() { return list_size; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM