Scala swing table - 捕获列点击

发布于 2024-09-30 21:03:05 字数 127 浏览 3 评论 0原文

我对 swing 和 scala 都很陌生,但我试图捕获在 scala.swing.Table 中单击列时的情况,但我一直无法弄清楚如何执行此操作。可以在scala中完成吗?我仍在学习如何阅读 scala API,因此任何见解都会有所帮助,

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 would be helpful,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗梦空间 2024-10-07 21:03:05

看起来 Scala Swing 尚不支持表头事件,因此您可能需要执行类似以下操作,将鼠标侦听器直接添加到 JTableHeader

我确信有一种优雅的方法来创建 scala.swing.Publisher 将侦听器与 listenToreactions 机制集成,它只是取决于是否值得付出额外的努力。

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
  }
}

此外,我发现 TableColumnSelected 事件提供的范围信息相当奇怪,它包含从当前单击到不同列中的上一次单击的连续列索引。也许我错过了一些关于原因的事情。

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.

永不分离 2024-10-07 21:03:05

我能够采用 Don Mackenzie 的示例并使用 Publisher 和 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)

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)
好听的两个字的网名 2024-10-07 21:03:05

请不要编写自己的表格排序实现 - 请改用 GlazedLists 。你会感谢我的。您可能需要使用标准 JTable 而不是 swing.Table,但它可以处理您必须执行的所有排序和过滤操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文