开发者

Scala swing table - capturing column clicks

I am pretty new to both swing and scala, but I am trying to capture when a column is clicked in a scala.swing.Table and I have been unable to figure out how to do this. Can it be done in scala? I am still learning how to read the scala API, so any insight wo开发者_C百科uld be helpful,


It looks like Scala Swing doesn't support table header events yet, so you might have to do something like the following which adds a mouse listener directly to the JTableHeader.

I'm sure there is an elegant way to create a scala.swing.Publisher to integrate the listener with the listenTo and reactions mechanism, it just depends if it is worth the extra effort.

import java.awt.Dimension

import java.awt.event.{MouseEvent, MouseAdapter}
import swing._
import event.TableColumnsSelected

object TableQuestion extends SimpleSwingApplication {
  val headers = Array.tabulate(10){"Col-"+_}.toSeq
  val rowData = Array.tabulate[Any](10, 10){""+_+":"+_}

  lazy val ui = new BoxPanel(Orientation.Vertical) {
    val table  = new Table(rowData, headers)
    val label  = new Label("")
    val header = table.peer.getTableHeader()

    header.addMouseListener(new MouseAdapter() {
      override def mouseClicked(e: MouseEvent) {
        val col = header.columnAtPoint(e.getPoint())

        label.text = "Column header "+col+" selected"
      }
    })    

    listenTo(table.selection)

    reactions += {
      case TableColumnsSelected(_, r, _) =>
        label.text = "Column "+table.selection.columns.leadIndex+" selected, range = "+r
    }
    contents  += new ScrollPane(table)
    contents  += label
  }

  def top = new MainFrame {
    title         = "Table Column Selection"
    preferredSize = new Dimension(400, 260)
    contents      = ui
  }
}

Additionally I find the range information provided by the TableColumnSelected event rather strange, it contains contiguous column indices from the current click to the previous click in a different column. Perhaps I'm missing something as to why.


I was able to take the example by Don Mackenzie and produce something using a Publisher and Reactor.

import java.awt.event.{MouseEvent, MouseAdapter}
import javax.swing.UIManager
import swing._
import event.TableEvent

object TableQuestion extends SimpleSwingApplication
{
  val headers = Array.tabulate(10) {"Col-" + _}.toSeq
  val rowData = Array.tabulate[Any](10, 10) {"" + _ + ":" + _}

  lazy val ui = new BoxPanel(Orientation.Vertical) {
  val table = new Table(rowData, headers)
  val label = new Label("")
  val header = table.peer.getTableHeader()

  header.addMouseListener(new MouseAdapter() {
    override def mouseClicked(e: MouseEvent) {
       publish(ColumnHeaderClicked(table, header.columnAtPoint(e.getPoint())))
    }
  })

  listenTo(table)

  reactions += {
    case ColumnHeaderClicked(_, column) => label.text = "Column header " + column + " selected"
  }
  contents += new ScrollPane(table)
  contents += label
}

  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName);
  def top = new MainFrame {
    title = "Table Column Selection"
    preferredSize = new Dimension(400, 260)
    contents = ui
  }
}   

case class ColumnHeaderClicked(override val source: Table, column: Int) extends TableEvent(source)


Please don't write your own implementation of table sorting - use GlazedLists instead. You will thank me. You'll probably need to use a standard JTable rather than a swing.Table, but it can handle all of the sorting and filtering you will ever have to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜