Thursday, March 3, 2011

How could I allow only unique strings in a JList ?

I have a JList that holds a bunch of strings . I would like it to act like a Set , so that it will only hold unique values . Is there any way to achieve this without checking that a new string doesn't already exist there ?

From stackoverflow
  • Hi,

    take a look at the docs: 1.4.2 | Java 6

    You can set your own ListModel via JList#setModel(ListModel) which might be backed by e.g. a HashSet instead of the Vector which is used by default.

    See also ListModel and AbstractListModel

    Tom Hawtin - tackline : LinkedHashSet would make more sense, but it'd still be an awkward O(n^2) operation to, say, repaint the widget.

0 comments:

Post a Comment