• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple JLists make only 2 of them selectable  RSS feed

 
Douby Luka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a GUI where the user can create some JList. I want to make only 2 of them selectable.
Example: JList1 is already selected - user select value in JList2 - then user select value in JList3 -> selecten of JList1 should be cleared.
When I run the code like below in MouseListener then everything is fine, but when I run it in an ListSelectionListener it is throwing an error "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1".
Does anyone know why error is throwen?

thanks

 
Brian Cole
Author
Ranch Hand
Posts: 936
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JList.getSelectedIndex() can return -1 (check the documentation) so your code needs to be able to handle that.

other comments/suggestions:
1) addMouseListener() is not really the correct way to do this. Check out addListSelectionListener().
2) Rather than walk the Components in a Container and instanceof/cast to JList, why not just add all your JList instances to a java.util.List<JList> (such as ArrayList) and iterate through that?

 
Douby Luka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks for your answer.

1) Thats why I am asking getSelectedIndex is returning correct value, the exception is throwen here:


When I add ListselectionListener, catch the exception like above and return nothing it is working. But is that the correct way to handel this?

2) The user is creating this JLists by himself so there are also multiple arrays of lists. Thats why i choose this way.

Here same code as ListSelectionListener. I added to JList not to ListSelectionModel. Is there a difference?

 
Brian Cole
Author
Ranch Hand
Posts: 936
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Douby Luka wrote:getSelectedIndex is returning correct value


Right, but the code you have exhibited never checks if (selectedIndex < 0), and I think you probably should be doing so after every call to JList.getSelectedIndex(), including in your ListSelectionHandler.

the exception is throwen here:


It's weird if calling JList.clearSelection() throws an ArrayIndexOutOfBoundsException with -1, given that you haven't registered any ListSelectionListeners. It might be interesting to see the stack trace.

Are you using the stock selection model? [Does getSelectionModel().getClass().getName() return DefaultListSelectionModel?] Also, which selection mode (SINGLE_SELECTION, SINGLE_INTERVAL_SELECTION, MULTIPLE_INTERVAL_SELECTION) are you using?


catch the exception like above and return nothing it is working. But is that the correct way to handel this?


I think it would be preferable to not have to catch Exception.


Here same code as ListSelectionListener. I added to JList not to ListSelectionModel. Is there a difference?


There is a difference, but it can be subtle. However it will determine what e.getSource() returns. You probably want e.getSource() to return the JList, not the ListSelectionModel.

I see that you're calling currentListSelected.getSelectedIndex(), but you should probably be calling e.getFirstIndex() instead. Possibly also e.getLastIndex(). Assuming it's >= 0 you can then call currentListSelected.isSelectedIndex(theIndex) to determine if this event is a selection or a deselection. On deselections you probably don't want clear anything. Keep in mind that when you clear selections on other lists, your listener will be notified of those very same deselections.
 
Douby Luka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's weird if calling JList.clearSelection() throws an ArrayIndexOutOfBoundsException with -1, given that you haven't registered any ListSelectionListeners. It might be interesting to see the stack trace.




If I use getFirstIndex() it is not working. When I select Element(0) and then select Element(1) it returns 0 in both cases.
I would need some help to get it work, it is sucking me off... -.-
How can I identify if it is a diselection?



 
Brian Cole
Author
Ranch Hand
Posts: 936
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You certainly have a way of turning a phrase.

A stack trace is a list of method names (and often line numbers) one can view via System.out.println(e.getStackTrace()). (See example here.) It's helpful in diagnosing where exceptions come from.

Douby Luka wrote:How can I identify if it is a diselection?

I was thinking about this overnight, and I don't think my advice above to examine e.getFirstIndex() or e.getLastIndex() is helpful. Sorry about that. I think all your ListSelectionListener needs to do is
That should work even under multiple selection.

I originally wrote, Your code is setting SINGLE_SELECTION mode, so it could simply call source.isSelectionEmpty() to determine selection vs. deselection. But that won't necessarily work. If you change a List's selection, you will probably receive a single event for the simultaneous selection and deselection [so e.getFirstIndex() will not be equal to e.getLastIndex()] but this is not absolutely guaranteed. If it instead sends you two separate events, by the time you receive them source.isSelectionEmpty() will return true for both.
 
Douby Luka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is I was looking for. Many thanks bro!!!
Now everything is working well but exception is throwen.

Do you know why?
Sry I forgot:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!