में इडियोमैटिक टेबल सेल रेंडरर scala.swing.Table
में रेंडरर प्रदान करने के लिए पारंपरिक जावा TableCellRenderer
दृष्टिकोण का उपयोग कर रहा था, जहां मैं तालिका के TableColumnModel
पर अपने प्रस्तुतकर्ता घोषित करता हूं। इस के लिए कोड देखा की तरह:स्कैला
val myTable = new Table {
lazy val tcm = initColumnModel
peer.setColumnModel(tcm)
override
protected def rendererComponent(sel: Boolean, foc: Boolean, row: Int, col: Int) = {
//GET THE VALUE FROM THE TableModel
val value = model.getValueAt(
peer.convertRowIndexToModel(row),
peer.convertColumnIndexToModel(col))
//GET THE RENDERER FROM THE ColumnModel
val renderer = tcm.getColumn(col).getCellRenderer
//WRAP IN A COMPONENT
Component.wrap(renderer.getTableCellRendererComponent(
peer,
value,
sel,
foc,
row,
col).asInstanceOf[JComponent])
}
}
दुर्भाग्य से यह एक स्मृति रिसाव के लिए प्रकट होता - शायद क्योंकि मैं तालिका में हर कोशिका के लिए एक नया घटक उदाहरण (~ 30k पंक्तियों के लिए) बनाने रहा हूँ। निश्चित रूप से जब मैं JTable
(उसी कॉलम और डेटा मॉडल) का उपयोग करके अपनी स्कैला तालिका को प्रतिस्थापित करता हूं) मेरी मेमोरी लीक दूर हो जाती है।
इसलिए मेरा प्रश्न यह है कि rendererComponent
विधि को ओवरराइड करते समय लोग किस प्रकार के कोड का उपयोग करते हैं, मानते हैं कि किसी के पास स्वयं का सेल रेंडरर है?
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)
}
}
इस मामले prepare
में एक विधि आप अपने खुद के रेंडरर पर परिभाषित करेगा है:
मुझे लगता है कि आप चाहते हैं 'peer.convertColumnIndexToModel (col) 'peer.convertColumnIndexToModel (पंक्ति)' –
के बजाय 'आप 'scala.swing.Table.viewToModelColumn (Int): Int' का भी उपयोग कर सकते हैं। ध्यान दें कि पंक्तियों के लिए समकक्ष रैपर विधि क्यों नहीं है .. –