[Logo]
Forums Register Login
Advice on fixing the index out of bounds error



Is it ok to disable a listener or should I go back to the drawing board?
Could you provide some more context? Because it's really hard to understand what your intention is with this code and from just a few lines of code, we can't know why and when you get an index out of bounds error.

TellTheDetails
The remove function on the jlist GUI -1's from the index. If I try and get the current get selected list it throws an exception if the value is -1.Thus, my solution was to change the value to the last index value e.g 3 or however many in the list. My problem is when I get to the last value I want the listener to stop listening, so that the exception will not be thrown? Sorry if I make no sense I have Aspergers syndrome. I might also want to restart listening I guess. Is it possible to stop and start listening? or is this bad practise?
It's still hard to understand exactly what you're after.

JList.getSelectedIndex() returns -1 if no elements in the list are selected.
JList.getLastVisibleIndex() returns -1 if nothing is visible or the list is empty.

I guess that what you want to do is (I'm not sure, you haven't told us):

Get the index of the selected element. If nothing is selected, get the index of the last visible item in the list. Then set the text of employeeName based on the index that was found. If nothing is selected or nothing is visible or the list is empty, you probably want to skip that last step. You can do that by simply checking if the index is not -1.

Thank you very much. I was getting confused. It now works with your code, problem solved.
I had thought it was always -1 when I was removing an index.
I will comment my code next time and provide a full description.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 943 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jan 19, 2018 04:55:17.