find checkboxlist last checked/unchecked item[solved]

This is the forum for JIDE Common Layer which is open sourced at https://github.com/jidesoft/jide-oss. Please note, JIDE technical support doesn't monitor this forum as often as other forums. Please consider subscribe for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Moderator: JIDE Support

Forum rules
Community driven forum for open source JIDE Common Layer. JIDE technical support doesn't monitor this forum as often as other forums. If you only use JIDE Common Layer, please consider subscribing for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

find checkboxlist last checked/unchecked item[solved]

Postby arthur524 » Wed May 13, 2009 7:15 pm

when using checkboxlist, i seemed to be unable to find a way to get the currently checked/unchecked item.
it didn't matter whether i used e.getFirstIndex() or e.getLastIndex().
when check/uncheck item 1, result was 1,
when check/uncheck item 2, sometimes resulted in 1, sometimes resulted in 2.
was this a bug, or did i do someting wrong?
Please advice, thanks!

btw: I used jide-oss version 2.6.2

-----------------------------------------

final DefaultListModel listModel = new DataListModel();
listModel.addElement(1);
listModel.addElement(2);
listModel.addElement(3);
listModel.addElement(4);
listModel.addElement(5);
final CheckBoxList checkBoxList = new CheckBoxList(listModel);
checkBoxList.getCheckBoxListSelectionModel().
addListSelectionListener(new ListSelectionListener() {

public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
System.out.println(listModel.getElementAt(e.getFirstIndex()));
} // end if

}
});
Last edited by arthur524 on Thu May 14, 2009 6:54 pm, edited 1 time in total.
arthur524
 
Posts: 13
Joined: Wed May 13, 2009 7:00 pm

Re: find checkboxlist checked value

Postby JIDE Support » Wed May 13, 2009 7:21 pm

Please check out our CheckBoxListDemo and uncomment the following codes then see if it is what you want.

Code: Select all
                if (!e.getValueIsAdjusting()) {
                    int[] selected = list.getCheckBoxListSelectedIndices();
                    for (int i = 0; i < selected.length; i++) {
                        int select = selected[i];
                        System.out.print(select + " ");
                    }
                    System.out.println("\n---");
                }


Thanks,
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37280
Joined: Sun Sep 14, 2003 10:49 am

Re: find checkboxlist checked value

Postby arthur524 » Wed May 13, 2009 8:22 pm

thanks for the reply, that's a pretty quick one! :D
I tested the code you posted, sorry to say it didn't work the way i expected.
I wanted to find the last checked/unchecked item, not all the item in selection
like this:
1, I've already checked item 2, 3, 4,
2, when I check item 1 then the last checked/unchecked item is item 1(item 1 is what i want)
3, when I uncheck item 1 then the last checked/unchecked item is item 1 (item 1 is what i want)

hope I made my problem clear to you.
arthur524
 
Posts: 13
Joined: Wed May 13, 2009 7:00 pm

Re: find checkboxlist checked value

Postby JIDE Support » Wed May 13, 2009 8:26 pm

In this case, I'm afraid you will have to record the last selection indices by yourself and compare with the latest one. In this way, you can get the latest changed index.

Thanks,
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37280
Joined: Sun Sep 14, 2003 10:49 am

Re: find checkboxlist checked value

Postby arthur524 » Wed May 13, 2009 9:09 pm

thanks, I'll try it out.
arthur524
 
Posts: 13
Joined: Wed May 13, 2009 7:00 pm

Re: find checkboxlist checked value

Postby arthur524 » Thu May 14, 2009 6:24 pm

I got it worked out. in case anyone run into the same problem as I did, I post source code here.
some caveats though:
    1, doesn't work with selectAll(), selectNone(), only find last checked/unchecked item.
    2, can't say I tested thoroughly.

just register your LastSelectedIndexChangeListener, event object contains information such as last checked/unchecked index and value, whether it's checked or unchecked.

feel free to use the code or improve it. :D

Code: Select all
import java.util.Arrays;

import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.EventListenerList;

import com.jidesoft.swing.CheckBoxList;

/**
 *
 * @author root
 */
public class FCheckBoxList extends CheckBoxList {

    private LastSelectedIndexHelper lastSelectedIndexHelper =
            new LastSelectedIndexHelper();
    private EventListenerList listenerList = new EventListenerList();

    public FCheckBoxList() {
        super();
        initialize();
    }

    public FCheckBoxList(ListModel listModel) {
        super(listModel);
        initialize();
    }

    public void addLastSelectedIndexChangeListener(
            LastSelectedIndexChangeListener listener) {
        listenerList.add(LastSelectedIndexChangeListener.class, listener);
    }

    public void removeLastSelectedIndexChangeListener(
            LastSelectedIndexChangeListener listener) {
        listenerList.remove(LastSelectedIndexChangeListener.class, listener);
    }

    private void initialize() {
        getCheckBoxListSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                lastSelectedIndexHelper.process(getCheckBoxListSelectedIndices());
                fireLastSelectedIndexChanged(this,
                        lastSelectedIndexHelper.getLastSelectedIndex(),
                        lastSelectedIndexHelper.isChecked(),
                        lastSelectedIndexHelper.getLastSelectdValue());
            }
        });
    }

    private void fireLastSelectedIndexChanged(Object source, int index,
            boolean checked, Object value) {
        LastSelectedIndexChangeListener[] listeners =
                listenerList.getListeners(LastSelectedIndexChangeListener.class);
        LastSelectedIndexChangeEvent event = new LastSelectedIndexChangeEvent(
                source, index, checked, value);
        for (LastSelectedIndexChangeListener listener : listeners) {
            listener.indexChanged(event);
        } // end for
    }

    private class LastSelectedIndexHelper {

        private int[] oddIndices;
        private int[] evenIndices;
        private boolean flipped;
        private boolean checked;
        private int lastSelectedIndex;

        public LastSelectedIndexHelper() {
            oddIndices = new int[0];
            evenIndices = new int[0];
            flipped = false;
            lastSelectedIndex = -1;
        }

        public boolean isChecked() {
            return checked;
        }

        public int getLastSelectedIndex() {
            return lastSelectedIndex;
        }

        public Object getLastSelectdValue() {
            if (lastSelectedIndex >= 0) {
                return getModel().getElementAt(getLastSelectedIndex());
            } else {
                return null;
            } // end if
        }

        public void process(int[] selected) {
            if (!flipped) {
                setOddIndices(selected);
            } else {
                setEvenIndices(selected);
            } // end if
            findLastSelectedIndex(selected);
        }

        private void setOddIndices(int[] selected) {
            this.oddIndices = Arrays.copyOf(selected, selected.length);
            flipped = true;
        }

        private void setEvenIndices(int[] selected) {
            this.evenIndices = Arrays.copyOf(selected, selected.length);
            flipped = false;
        }

        private int[] getLastIndices() {
            if (flipped) {
                return evenIndices;
            } else {
                return oddIndices;
            } // end if
        }

        private void findLastSelectedIndex(int[] selected) {
            int[] latter = getLastIndices();
            Arrays.sort(selected);
            Arrays.sort(latter);
            for (int value : selected) {
                if (Arrays.binarySearch(latter, value) < 0) {
                    checked = true;
                    lastSelectedIndex = value;
                    return;
                } // end if
            } // end for
            for (int value : latter) {
                if (Arrays.binarySearch(selected, value) < 0) {
                    checked = false;
                    lastSelectedIndex = value;
                    return;
                } // end if
            }
            lastSelectedIndex = -1;
        }
    }
}



import java.util.EventObject;

/**
 *
 * @author root
 */
public class LastSelectedIndexChangeEvent extends EventObject {

    private int index = -1;
    private boolean checked = false;
    private Object value = null;

    public LastSelectedIndexChangeEvent(Object source, int index,
            boolean checked, Object value) {
        super(source);
        this.index = index;
        this.checked = checked;
        this.value = value;
    }

    public int getIndex() {
        return index;
    }

    public boolean isChecked() {
        return checked;
    }

    public Object getValue() {
        return value;
    }
}


import java.util.EventListener;

/**
 *
 * @author root
 */
public interface LastSelectedIndexChangeListener extends EventListener {

    public void indexChanged(LastSelectedIndexChangeEvent event);
}

arthur524
 
Posts: 13
Joined: Wed May 13, 2009 7:00 pm

Re: find checkboxlist checked value

Postby JIDE Support » Thu May 14, 2009 6:27 pm

Thanks for your code. If anybody has similar question later on, we will refer your post to him/her.

Thanks,
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37280
Joined: Sun Sep 14, 2003 10:49 am


Return to JIDE Common Layer Open Source Project Discussion (Community Driven)

Who is online

Users browsing this forum: No registered users and 35 guests