The idiomatic way of using Scala table cell renderers is to use Table.AbstractRenderer
(if implementing your own) or one of its subclasses:
val tcr = new Table.AbstractRenderer[MyObj, MyRenderer](new MyRenderer) {
def configure(t: Table, sel: Boolean, foc: Boolean, o: MyObj, row: Int, col: Int) = {
//component variable is bound to your renderer
component.prepare(o)
}
}
In this case prepare
is a method you would define on your own renderer class:
class MyRenderer extends Label {
def prepare(o: MyObj) {
text = o.toString //or whatever
}
}
Then this is used by overriding the rendererComponent
method on Table
:
val t = new Table {
override def rendererComponent(sel: Boolean, foc: Boolean, row: Int, col: Int) = {
//FIND VALUE
val v = model.getValueAt(
peer.convertRowIndexToModel(row),
peer.convertColumnIndexToModel(row))
col match {
case 0 => tcr.componentFor(this, sel, foc, v, row, col)
}
}
}
Scala comes with its own implementations of AbstractRenderer
, namely LabelRenderer
which takes a function as an argument, converting an instance of MyObj to a Tuple2
consisting of a String
and an Icon
, for that label to display:
val ltcr = new LabelRenderer[MyObj] ( (o: MyObj) => (null, o.toString) )
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…