Removing Item From JList - getSelectedIndex problem  
Author Message
CBO





PostPosted: 2008-4-9 18:21:00 Top

java-programmer, Removing Item From JList - getSelectedIndex problem 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:

int index = lstComputerExceptions.getSelectedIndex();
System.out.println("index is: " + index);
model.remove(0);

int size = model.getSize();

if (size == 0) { //Nobody's left, disable firing.
btnDelete.setEnabled(false);

} else { //Select an index.
if (index == model.getSize()) {
//removed item in last position
index--;
}

lstComputerExceptions.setSelectedIndex(index);
lstComputerExceptions.ensureIndexIsVisible(index);
}

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
 
CBO





PostPosted: 2008-4-10 16:40:00 Top

java-programmer >> Removing Item From JList - getSelectedIndex problem On 9 Apr, 11:21, CBO <email***@***.com> 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:
>
> 爄nt index = lstComputerExceptions.getSelectedIndex();
> ????System.out.println("index is: " + index);
> ????model.remove(0);
>
> ????int size = model.getSize();
>
> ????if (size == 0) { //Nobody's left, disable firing.
> ??????btnDelete.setEnabled(false);
>
> ????} else { //Select an index.
> ??????if (index == model.getSize()) {
> ????????//removed item in last position
> ????????index--;
> ??????}
>
> ??????lstComputerExceptions.setSelectedIndex(index);
> ??????lstComputerExceptions.ensureIndexIsVisible(index);
> ????}
>
> 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
 
Lew





PostPosted: 2008-4-10 19:53:00 Top

java-programmer >> Removing Item From JList - getSelectedIndex problem 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:
>>
>> int index = lstComputerExceptions.getSelectedIndex();
>> System.out.println("index is: " + index);
>> model.remove(0);
>>
>> int size = model.getSize();
>>
>> if (size == 0) { //Nobody's left, disable firing.
>> btnDelete.setEnabled(false);
>>
>> } else { //Select an index.
>> if (index == model.getSize()) {
>> //removed item in last position
>> index--;
>> }
>>
>> lstComputerExceptions.setSelectedIndex(index);
>> lstComputerExceptions.ensureIndexIsVisible(index);
>> }
>>
>> 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! What was the answer?

--
Lew
 
 
CBO





PostPosted: 2008-4-10 20:50:00 Top

java-programmer >> Removing Item From JList - getSelectedIndex problem On 10 Apr, 12:53, Lew <email***@***.com> 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:
>
> >> 爄nt index = lstComputerExceptions.getSelectedIndex();
> >> ????System.out.println("index is: " + index);
> >> ????model.remove(0);
>
> >> ????int size = model.getSize();
>
> >> ????if (size == 0) { //Nobody's left, disable firing.
> >> ??????btnDelete.setEnabled(false);
>
> >> ????} else { //Select an index.
> >> ??????if (index == model.getSize()) {
> >> ????????//removed item in last position
> >> ????????index--;
> >> ??????}
>
> >> ??????lstComputerExceptions.setSelectedIndex(index);
> >> ??????lstComputerExceptions.ensureIndexIsVisible(index);
> >> ????}
>
> >> 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! ?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