Re: Removing Item From JList - getSelectedIndex problem
by CBO <christopher_board@[EMAIL PROTECTED]
>
Apr 10, 2008 at 05:49 AM
On 10 Apr, 12:53, Lew <l...@[EMAIL PROTECTED]
> wrote:
> CBO wrote:
> >> 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(inde=
x);
> >> =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.
> > This issue has been resolved
>
> Don't keep us in suspense! =A0 What was the answer?
>
> --
> Lew- Hide quoted text -
>
> - Show quoted text -
I had accidently initialized the JList model twice so it kept on
causing it to go wrong. Deleted it and now it works fine. I wasn't
that bright lol