Background
In this posting, I see that a change listener can be used on a JTabbedPane
to detect the index i
of the newly activated tab.
In the Scala docs, I can see that for a TabbedPane
named tp
, tp.selection.index
will give me the i
that I need, but how do I wire up the code to listen properly?
I tried following the reactions-listenTo
paradigm that Scala seems to use, but ran into problems finding the appropriate event type. If you click on this link and click on known subclasses, there is a list of events that I can plug into reactions, but none of them seem to indicate TabChanged.
Below is my attempt to get this to work.
CODE
private val tp = new TabbedPane() {
pages += new TabbedPane.Page("Deck0",new ScrollPane(tables(0)))
pages += new TabbedPane.Page("Deck1",new ScrollPane(tables(1)))
reactions += { // not sure what to put under reactions
case e => println("%s => %s" format(e.getClass.getSimpleName, e.toString))
}
}
listenTo( tp.selection ) // don't know if this is right
UPDATE
I have managed to google the code for TabbedPane.scala. The pertinent snippet is below - I'm trying to figure out the implications for my issue.
/**
* The current page selection
*/
object selection extends Publisher {
def page: Page = pages(index)
def page_=(p: Page) { index = p.index }
def index: Int = peer.getSelectedIndex
def index_=(n: Int) { peer.setSelectedIndex(n) }
peer.addChangeListener(new javax.swing.event.ChangeListener {
def stateChanged(e: javax.swing.event.ChangeEvent) {
publish(SelectionChanged(TabbedPane.this))
}
})
}