0

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.

GM-Xile GM-Xile
  • 321
  • 1
  • 8
  • 21

2 Answers2

0

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

0

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; }

private void getListSize() { return list_size; }

Neo Perez
  • 1
  • 1