0

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))
      }
    })
  }
Community
  • 1
  • 1
kfmfe04
  • 14,936
  • 14
  • 74
  • 140

1 Answers1

1

Further researching the UPDATE that I added to the OP (source tells all) along with this additional information, I was able to work a solution simply:

import swing.event.SelectionChanged

private val tp = new TabbedPane() {
  pages += new TabbedPane.Page("Deck0",new ScrollPane(tables(0)))
  pages += new TabbedPane.Page("Deck1",new ScrollPane(tables(1)))
}

reactions += {
  case SelectionChanged( x ) => println( "changed to %d" format(tp.selection.index))
  case e => println("%s => %s" format(e.getClass.getSimpleName, e.toString))
}

listenTo( tp.selection )  // this is required
kfmfe04
  • 14,936
  • 14
  • 74
  • 140