如何嵌入在斯卡拉一个Swing表一(工作)按钮? [英] How to embed a (working) Button in a Swing Table in Scala?

查看:159
本文介绍了如何嵌入在斯卡拉一个Swing表一(工作)按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用斯卡拉/ Swing的创建一个表,其列被​​填充的一个按钮秒。

I'm trying to use Scala/Swing to create a Table, one of whose columns is populated by Buttons.

我的出发点是<一个href=\"http://my.safaribooksonline.com/book/programming/scala/9780981531649/programming-in-scala/chapter_35_the_scells_s$p$padsh\"相对=nofollow> SCells US preadsheet例如,从Odersky的等人的的一>,特别是使用 rendererComponent 控制组件出现在每个单元格。

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?

我见过这个问题(和这个),并曾尝试以下办法有(使用 Table.AbstractRenderer ),但也没有工作 - 这是不是在所有明显,我在那里把按钮点击反应,该版本。 (那是过时的做法吗?或者是从Scala的书太simplisitic的办法?)

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?)

感谢您的任何意见!

推荐答案

您可以通过提供一个自定义的表格模型使列ineditable。然而,你的手机必须是可编辑的,因为那是编辑组件成为唯一的出路'活'(重绘状态变化,接收鼠标事件)。

You can make a column ineditable by providing a custom table model. However, your cell must be editable, because that is the only way the editing component becomes 'live' (repaints state changes, receives mouse events).

在正常渲染(使用 renderComponent ),组件仅用于邮票它,即表只是调用油漆的组件。因此,性能明智的,你应该重新使用每个渲染组件的一个实例,而不是创建一个新的标签 / 按钮在每个呼叫。

In the normal rendering (using renderComponent), the component is only used to 'stamp' it, i.e. the table just calls paint on the component. Thus, performance-wise, you should re-use one instance of each rendering component, instead of creating a new Label / Button in every call.

所以,你需要重写编辑方法。不幸的是它返回一个纯 javax.swing.table.TableCellEditor ,因此必须下台,以平原的javax.swing 东西和松散的所有斯卡拉善良...

So, you need to override the editor method. Unfortunately it returns a plain javax.swing.table.TableCellEditor, and thus you must step down to the plain javax.swing stuff and loose all the Scala goodness...

以下几乎起作用。奇怪的是,按钮,点击它的时候消失 - 不知道为什么: - (

The following almost works. Strangely, the button disappears when clicking on it -- have no idea why :-(

import scala.swing._
import scala.swing.event._
import javax.swing.{AbstractCellEditor, JTable}
import javax.swing.table.TableCellEditor
import java.awt.{Component => AWTComponent}

class TableButtons extends ScrollPane {
  private val lb = new Label("")
  private val b  = new Button

  private val buttonEditor = new AbstractCellEditor with TableCellEditor {
    listenTo(b)
    reactions += {
      case ButtonClicked(`b`) => 
        println("Clicked")
        fireEditingStopped()
    }
    def getCellEditorValue: AnyRef = "what value?"
                               // ouch, we get JTable not scala.swing.Table ...
    def getTableCellEditorComponent(tab: JTable, value: AnyRef, isSelected: Boolean,
                                       row: Int, col: Int): AWTComponent = {
      b.text = "Click!"
      b.peer  // ouch... gotta go back to AWT
    }
  }

  viewportView = new Table(2, 2) {
    rowHeight = 25
    override def rendererComponent(isSelected: Boolean, hasFocus: Boolean,
                                   row: Int, column: Int): Component =
      if (column == 0) {
        lb.text = "Hello"
        lb
      } else {
        b.text = "Click?"
        b
      }

    override def editor(row: Int, col: Int): TableCellEditor =
      if (col == 1) buttonEditor else super.editor(row, col)
  }
}

val top = new Frame {
  title = "Table button test"
  contents = new TableButtons
  pack()
  visible = true
}

在任何情况下,请检查甲骨文JTable的教程渲染器和编辑器的复杂细节。

In any case, check the Oracle JTable tutorial for the intricate details of renderers and editors.

这篇关于如何嵌入在斯卡拉一个Swing表一(工作)按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆