Re: Removing Item From JList - getSelectedIndex problem
by CBO <christopher_board@[EMAIL PROTECTED]
>
Apr 10, 2008 at 01:39 AM
On 9 Apr, 11:21, CBO <christopher_bo...@[EMAIL PROTECTED]
> wrote:
> Hi all,
>
> I am currently working on a JList that populates data into a JList
> from a CSV file. I have a button that is supposed to get the Selected
> index and then delete that item from the JList. Below is the code that
> I am using:
>
> =A0int index =3D lstComputerExceptions.getSelectedIndex();
> =A0 =A0 =A0 =A0 System.out.println("index is: " + index);
> =A0 =A0 =A0 =A0 model.remove(0);
>
> =A0 =A0 =A0 =A0 int size =3D model.getSize();
>
> =A0 =A0 =A0 =A0 if (size =3D=3D 0) { //Nobody's left, disable firing.
> =A0 =A0 =A0 =A0 =A0 =A0 btnDelete.setEnabled(false);
>
> =A0 =A0 =A0 =A0 } else { //Select an index.
> =A0 =A0 =A0 =A0 =A0 =A0 if (index =3D=3D model.getSize()) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 //removed item in last position
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 index--;
> =A0 =A0 =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 =A0 =A0 lstComputerExceptions.setSelectedIndex(index);
> =A0 =A0 =A0 =A0 =A0 =A0
lstComputerExceptions.ensureIndexIsVisible(index);=
> =A0 =A0 =A0 =A0 }
>
> I have printed out what index is and no matter what item I have in the
> Jlist I have selected it will always print out
> -1. Therefore keeps on causing my program to crash and not delete the
> item out of the Jlist.
>
> Any help in this matter would be highly appreicated.
>
> Thank you
This issue has been resolved