[英]How to dynamically add Nodes to VBox in ScalaFX
My ScalaFX app has a Button
that adds a GridPane
to a VBox
.我的 ScalaFX 应用程序有一个Button
,可将GridPane
添加到VBox
。 I tried directly updating the VBox
children in Button.onAction
but that causes the Button
to become non-responsive after rendering the first added GridPane
.我尝试直接更新Button.onAction
中的VBox
子级,但这会导致Button
在渲染第一个添加的GridPane
后变得无响应。 I am now trying the following code:我现在正在尝试以下代码:
import scalafx.scene.{Node, Scene}
import scalafx.application.JFXApp
import scalafx.geometry.Insets
import scalafx.scene.control.{Button, Label}
import scalafx.scene.layout.{GridPane, VBox}
import scalafx.Includes._
import scalafx.collections.ObservableBuffer
object Gui extends JFXApp {
stage = new JFXApp.PrimaryStage {
title = "DSP Lab"
scene = new Scene(320, 300) {
val bufferOfNodes = new ObservableBuffer[Node]
val vBox = new VBox {
children = bufferOfNodes // assign ObservableBuffer to children
}
val addGridPaneButton = new Button {
text = "Add GridPane to VBox"
onAction = _ => {
bufferOfNodes += getNewGridPane // update the ObservableBuffer
}
}
content = List(addGridPaneButton, vBox)
}
}
stage.setX(100)
stage.setY(100)
def getNewGridPane: GridPane = new GridPane {
vgap = 5
hgap = 10
padding = Insets(20)
val sampleFrequencyLabel = new Label("Sample Frequency:")
val sampleFrequencyField = new DoubleField
val amplitudeLabel = new Label("Amplitude:")
val amplitudeField = new DoubleField
add(sampleFrequencyLabel, 0, 0)
add(sampleFrequencyField, 1, 0)
add(amplitudeLabel, 0, 1)
add(amplitudeField, 1, 1)
}
}
By assigning VBox.children
to the ObservableBuffer[Node]
the Button
stays responsive and onAction
adds the GridPane
to bufferOfNodes
but the GridPane
s seem to never get added to the VBox
and they are never rendered.通过将VBox.children
分配给ObservableBuffer[Node]
Button
保持响应,并且onAction
将GridPane
添加到bufferOfNodes
但GridPane
似乎永远不会添加到VBox
并且它们永远不会被渲染。
Is there a way to do this?有没有办法做到这一点?
This was asked and answered on ScalaFX-Users mailing list: https://groups.google.com/forum/?utm_medium=email&utm_source=footer&pli=1#!topic/scalafx-users/3IzGAnflUnU这是在 ScalaFX 用户邮件列表中询问和回答的: https://groups.google.com/forum/?utm_medium=email&utm_source=footer&pli=1#!topic/scalafx-users/3IzGAnflUnU
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.