Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can make a column ineditable by providing a custom table model. However, your cell must be editable, because that is the only way the editing component becomes 'live' (repaints state changes, receives mouse events).</p> <p>In the normal rendering (using <code>renderComponent</code>), the component is only used to 'stamp' it, i.e. the table just calls <code>paint</code> on the component. Thus, performance-wise, you should re-use one instance of each rendering component, instead of creating a new <code>Label</code> / <code>Button</code> in every call.</p> <p>So, you need to override the <code>editor</code> method. Unfortunately it returns a plain <code>javax.swing.table.TableCellEditor</code>, and thus you must step down to the plain <code>javax.swing</code> stuff and loose all the Scala goodness...</p> <p>The following almost works. Strangely, the button disappears when clicking on it -- have no idea why :-(</p> <pre><code>import scala.swing._ import scala.swing.event._ import javax.swing.{AbstractCellEditor, JTable} import javax.swing.table.TableCellEditor import java.awt.{Component =&gt; AWTComponent} </code></pre> <p> </p> <pre><code>class TableButtons extends ScrollPane { private val lb = new Label("") private val b = new Button private val buttonEditor = new AbstractCellEditor with TableCellEditor { listenTo(b) reactions += { case ButtonClicked(`b`) =&gt; println("Clicked") fireEditingStopped() } def getCellEditorValue: AnyRef = "what value?" // ouch, we get JTable not scala.swing.Table ... def getTableCellEditorComponent(tab: JTable, value: AnyRef, isSelected: Boolean, row: Int, col: Int): AWTComponent = { b.text = "Click!" b.peer // ouch... gotta go back to AWT } } viewportView = new Table(2, 2) { rowHeight = 25 override def rendererComponent(isSelected: Boolean, hasFocus: Boolean, row: Int, column: Int): Component = if (column == 0) { lb.text = "Hello" lb } else { b.text = "Click?" b } override def editor(row: Int, col: Int): TableCellEditor = if (col == 1) buttonEditor else super.editor(row, col) } } </code></pre> <p> </p> <pre><code>val top = new Frame { title = "Table button test" contents = new TableButtons pack() visible = true } </code></pre> <p>In any case, check the <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/table.html" rel="nofollow">Oracle JTable tutorial</a> for the intricate details of renderers and editors.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload