I'm trying to use Scala/Swing to create a Table, one of whose columns is populated by Button
s.
My starting point is the SCells spreadsheet example from Odersky et al's book, and in particular the use of rendererComponent
to control the Component
appearing in each cell.
Unfortunately, while this creates a button successfully, the button is not clickable. Here's a reasonably minimal and self-contained example:
import swing._
import swing.event._
class TableButtons extends ScrollPane {
viewportView = new Table(2,2) {
rowHeight = 25
override def rendererComponent(isSelected: Boolean, hasFocus: Boolean,
row: Int, column: Int): Component =
if (column == 0) {
new Label("Hello")
} else {
val b = new Button { text = "Click" }
listenTo(b)
reactions += {
case ButtonClicked(`b`) => println("Clicked")
}
b
}
}
}
object Main extends SimpleSwingApplication {
def top = new MainFrame {
title = "Table button test"
contents = new TableButtons
}
}
When I run this, I get a table with two columns; the first contains labels, the second contains buttons, but the buttons aren't clickable.
Possibly related issue: the cells (including the ones containing buttons) are editable. What's the best way to disable editing?
I've seen this question (and this one) and have tried following the approach there (using Table.AbstractRenderer
) but that's also not working - and it's not at all obvious to me where to put reactions to button clicks in that version. (Is that approach outdated? Or is the approach from the Scala book too simplisitic?)
Thanks for any advice!