I am working with JList and I have encountered a few design problems.
what I want is a gui componenet that presents a list and lets the user add or remove values from it.
So I have created a class that receives a list
List<? exteds IDisplayable>
- IDisplayable is a simple interface that has String DisplayString(). every objects that wants to be displayed in the list needs to implement IDIsplayable.
When my GUI form loads I iterate over the list and do a
MylistModel.addElement(iDisplayable.getDisplayString()
This is because I don’t want it to display the toString(). So I added a method.
Now my question is how to return the list to the gui form that called it.
Should I iterate it and compare by name? This sounds awful.
I thing I need to put in my ListModel the object but display a different toString.
Should I create a new listmodel? I can’t even extend the AbstractListModel cause it uses toString.
Is that the only solution?
As already mentioned, a xxRenderer is the collaborator which decides about all visual representation of an item shown in a “collection component” (such as JList, JTable, etc …). When stuck to core Swing support, the way to go is a implementing a custom renderer. SwingX supports a more lightweight approach by allowing to plug-in string display (and visual decorations, but that’s a story different from this): implement a custom StringValue (SwingX speak for string converter) and pass it to a SwingX renderer like
In other words: SwingX supports a unified string representation throughout all of its collection components. The full power of that approach shows when sorting/filtering/searching: all that functionality automagically uses that custom string representation, that is the users by default sort/filter/search by what they see – no additional effort required by the developer 🙂