Search code examples
javaswingjpaneljlistlisteners

Java Swing - make two JLists "siblings" - i.e. only one item in either can be selected


I have a JPanel which contains two JLists - both can have an item in them selected - as expected.

What I'd love to be able to do is have it so that only one item in either to be selected

Can anyone help me with either

a) "marrying" them so this can be the case

b) giving me some tips for the best practice to write listeners which can preside over them both and unselect all the elements of one when the other is selected - I'd rather avoid this if possible as I can see it getting ugly!!

Thanks :)


Solution

  • I think the best solution, also for the user, is putting a radio button next with a category label to each list, so you clearly disable the other each time you select one.

    I can imagine the user clicking values on the first list, then clicking on the next one and seeing all the values he clicked are gone, with logical frustration...

    Then when you are taking the values from the form, just take the enabled ones