开发者

JTable - sorting rendered values

I am using custom renderer to render cell values in JTable. Wh开发者_开发技巧en I perform sorting using my own Comparator or TableRowSorter, values are generally sorted using data from the model.

Is there a way to sort columns on rendered cell values instead of original value?


When I perform sorting using my own Comparator

Then your custom Comparator needs to know what the rendered values will be and sort on those values. This means you need to convert the data from the TableModel to the rendered value in your Comparator.


Yes, use a TableRowSorter, look at the tutorial for an example:

http://download.oracle.com/javase/tutorial/uiswing/components/table.html#sorting


SwingX rendering mechanism does so automatically (WYSIWYS - what you see is what you sort/search). The only thing you have to supply is a StringValue: that's the SwingX speak for defining the representation of cell content

JXTable table = new JXTable(myModel); // creates a RowSorter automatically
StringValue sv = new StringValue() {
     @Override
     public String getString(Object value) {
          if (value instanceof MyClass) {
              return // whatever string you want to create from the valuee's property
          } 
          return StringValues.TO_STRING.getString(value);
     }
};
table.setDefaultRenderer(MyClass.class, new DefaultTableRenderer(sv));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜