问题
I'm working on simple UI that has a scala.swing.Table component. I'd like to sort the table rows using java.swing.table.TableRowSorter. The Table class doesn't provide any API for using a row sorter so I tried setting it directly on the peer
val table = new Table(height, width) {
  import javax.swing.table._
  rowHeight = 25
  autoResizeMode = Table.AutoResizeMode.NextColumn
  showGrid = true
  gridColor = new java.awt.Color(150, 150, 150)
  model = myModel
  peer.setRowSorter(new TableRowSorter(model))
}
Now when I click on the column headings I get the little up/down arrows but the table contents don't update with the new sort order. If there's a row selected when I click on the column heading the selection moves to the row where the selected should be in based on the sort order. I've added custom comparators that they get called as expected so the sorting is really happening but the table isn't being updated.
Am I missing something to get the table to update? Is it legitimate to access the underlying JTable peer object?
回答1:
Answering my own question.
The scala.swing.Table class doesn't support the row sorting features added in Java 6. See the Table.scala source file at line 277:
def apply(row: Int, column: Int): Any = model.getValueAt(row, viewToModelColumn(column))
// TODO: this is Java 6 stuff
// def apply(row: Int, column: Int): Any = model.getValueAt(viewToModelRow(row), viewToModelColumn(column))
//def viewToModelRow(idx: Int) = peer.convertRowIndexToModel(idx)
//def modelToViewRow(idx: Int) = peer.convertRowIndexToView(idx)
I moved the scala.swing files into a different package and built it into my project, then uncommented out the Java 6 stuff and commented out the old apply() method and now table sorting works.
Here's a link to the bug report on this. The Java 6 code was added back in August and then removed until the Scala library build switches to Java 6.
来源:https://stackoverflow.com/questions/9588765/using-tablerowsorter-with-scala-swing-table