2014-07-08 50 views
0

我面對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

難道第一列是0而不是1? – johanandren

回答

0

如果添加另一個

 case _ => tcr.componentFor(this, sel, foc, "foo", row, col) 

在比賽中def rendererComponent回退,它將沒有例外至少工作。

您的模型中有兩列,實際列索引將爲零和一;這些得到傳遞給rendererComponent


要得到一個完整的演示:

import swing._ 
import javax.swing.table.DefaultTableModel 

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 = o // changed 
     } 
    } 
    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 0 => tcr.componentFor(this, sel, foc, tableModel.getValueAt(row, 0).toString, row, col) // changed 
      case 1 => tcr.componentFor(this, sel, foc, tableModel.getValueAt(row, 1).toString, row, col) // changed 
     } 
     } 
    } 
    table.model = tableModel 
    val addButton = Button("add") { 
     tableModel.addRow(Array[Object]("text 1", "text 2")) // changed 
    } 

    panel.layout(table) = BorderPanel.Position.Center 
    panel.layout(addButton) = BorderPanel.Position.South 
    panel 
    } 

    def main(args: Array[String]) { 
    val mainFrame = new Frame() 
    mainFrame.preferredSize = new Dimension(800, 600) 
    mainFrame.contents = initPanel() 
    mainFrame.visible = true 

    } 
} 

這樣你添加到模型中的值在表中顯示出來。 當然在這個片段中兩個case聲明沒有多大意義;在這種情況下,match不是必需的,因爲您始終使用相同的渲染器組件。

+1

感謝解決問題,但異常似乎讓我感到困惑 –

+0

@MariuszMarciniak javax.swing.JTable'已經是一個混亂的地獄。添加一半的Scala門面,你的困惑增加了四倍:-O –

+0

@ 0__不能同意更多,但是沒有好的Scala GUI界面,這只是支持工具應用 –

相關問題