我面對scala.MatchError: 0 (of class java.lang.Integer)
當我嘗試新的行添加到表自定義渲染分量:scala.MatchError在轉檯rendererComponent
import java.awt.Dimension
import scala.swing.BorderPanel
import scala.swing.Button
import scala.swing.Component
import scala.swing.Panel
import scala.swing.Table
import javax.swing.table.DefaultTableModel
import scala.swing.Label
object MatchErrorInTable {
def initPanel() : Panel = {
val tableModel = new DefaultTableModel(Array[Object]("col1", "col2"), 0)
val panel = new BorderPanel
class MyRenderer extends Label {
def prepare(o: String) {
text = "foo"
}
}
val tcr = new Table.AbstractRenderer[String, MyRenderer](new MyRenderer) {
def configure(t: Table, sel: Boolean, foc: Boolean, o: String, row: Int, col: Int) = {
component.prepare(o)
}
}
val table = new Table(0, 2) {
override protected def rendererComponent(sel: Boolean, foc: Boolean, row: Int, col: Int): Component = {
col match {
case 1 => tcr.componentFor(this, sel, foc, "bar", row, col)
}
}
}
table.model = tableModel
val addButton = Button("add") {
tableModel.addRow(Array[Object]("text", null))
}
panel.layout(table) = BorderPanel.Position.Center
panel.layout(addButton) = BorderPanel.Position.South
panel
}
def main(args: Array[String]) {
val mainFrame = new MainFrame()
mainFrame.preferredSize = new Dimension(800, 600)
mainFrame.contents = initPanel
mainFrame.visible = true
}
}
例外:
Exception in thread "AWT-EventQueue-0" scala.MatchError: 0 (of class java.lang.Integer)
at com.mm.calendar.MatchErrorInTable$$anon$1.rendererComponent(MatchErrorInTable.scala:29)
at scala.swing.Table$$anon$2$$anon$10.getTableCellRendererComponent(Table.scala:116)
at scala.swing.Table$$anon$2$$anon$10.getTableCellRendererComponent(Table.scala:114)
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
如何有可能是我在rendererComponent中有java.lang.Integer?
我主要是立足我的解決方案上:
Idiomatic table cell renderers in Scala
我使用2.11.1版本。
難道第一列是0而不是1? – johanandren