开发者

JList - design question regarding the toString() presentation

开发者 https://www.devze.com 2023-02-24 09:43 出处:网络
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.

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

StringValue sv = new StringValue() {
      @Override
      public String getString(Object value) {
          if (value instanceof MyObject) {
              return .... // use MyObject properties to build a suitable rep
          }
          return TO_STRING.getString(value);
      }    
}
list.setCellRenderer(new DefaultListRenderer(sv));
// a bit of beauty: same rep is re-usable in other collection components
table.setCellRenderer(MyObject.class, new DefaultTableRenderer(sv);
comboBox.setRenderer(list.getCellRenderer());
tree.setTreeCellRenderer(new DefaultTreeRenderer(sv));

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 :-)


You could create a wrapper object that holds an IDisplayable object and implements a toString() method that just calls getDisplayString() on the IDisplayable. Might not be that nice, but could be be better than creating your own list model.

Edit: Not sure why this did not come up at first. But it seems like a ListCellRenderer is really what you need: http://download.oracle.com/javase/tutorial/uiswing/components/list.html


Should I iterate it and compare by name? sounds awful.

It is awful. You can ask for the index of the selected item, so you don't need to compare but you can immediately grab the right object. The question is if this quick and dirty approach is good enough. As you already mentioned a new list model would be the "clean" solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号