我正在爲家庭成員創建訂單管理系統。我需要應用程序能夠在運行時自行更新,當用戶選擇是否爲蛋糕,餅乾,蛋糕或其他。ScalaFXML應用程序:在運行時將組件添加到CheckBox上單擊
我一直在嘗試解決的方法是在窗體中添加一個空白窗格,然後點擊窗體填充位於另一個FXML文件中的組件。問題是這樣的:我的控制器中的字段全部設置爲空,所以當我嘗試更新窗格時,我得到一個空指針異常。我無法弄清楚爲什麼我的所有字段都是空的,但我懷疑它是@sfxml註釋與我設置的方式不相符。也許有更好的方法來完成這個工作。任何幫助表示讚賞。這是我迄今爲止;我會盡力給出一個可以證明的例子。在這裏我打算添加組件的面積是通過在表單中的評論可見,剛下的複選框:
的形式爲:Form.FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane id="rootLayout" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="500.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.FormController">
<center>
<VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<TilePane maxHeight="119.0" minHeight="100.0" prefHeight="119.0" prefWidth="485.0">
<children>
<HBox maxHeight="30.0" maxWidth="485.0" minHeight="10.0" prefHeight="14.0" prefWidth="485.0"
TilePane.alignment="CENTER">
<children>
<TextField fx:id="fNameField" maxHeight="30.0" minHeight="17.0" prefHeight="18.0"
prefWidth="156.0" promptText="first name"/>
<TextField fx:id="lNameField" maxHeight="30.0" minHeight="30.0" promptText="last name"/>
<TextField fx:id="dateField" maxHeight="30.0" minHeight="26.0" prefHeight="30.0"
prefWidth="151.0" promptText="due date"/>
</children>
</HBox>
<HBox maxHeight="30.0" maxWidth="485.0" minHeight="10.0" prefHeight="14.0" prefWidth="485.0"
TilePane.alignment="CENTER">
<children>
<TextField fx:id="phoneNumber" maxHeight="30.0" minHeight="17.0" prefHeight="18.0"
prefWidth="156.0" promptText="phone number"/>
<TextField fx:id="email" maxHeight="30.0" minHeight="30.0" prefHeight="30.0"
prefWidth="331.0" promptText="e-mail"/>
</children>
</HBox>
<TextField fx:id="streetField" maxHeight="30.0" minHeight="30.0" promptText="street"/>
<HBox maxHeight="44.0" minHeight="0.0" prefHeight="27.0" prefWidth="485.0">
<children>
<TextField fx:id="cityField" maxHeight="30.0" minHeight="30.0" prefHeight="30.0"
prefWidth="374.0" promptText="city"/>
<TextField fx:id="stateField" maxHeight="30.0" minHeight="30.0" prefHeight="30.0"
prefWidth="109.0" promptText="state"/>
<TextField fx:id="zipField" maxHeight="30.0" minHeight="30.0" prefHeight="30.0"
prefWidth="136.0" promptText="zip"/>
</children>
</HBox>
</children>
</TilePane>
<HBox maxHeight="65.0" minHeight="30.0" prefHeight="33.0" prefWidth="485.0" spacing="95.0">
<children>
<CheckBox fx:id="cakeCB" mnemonicParsing="false" text="CK" onAction="#checkboxSelected">
<HBox.margin>
<Insets top="8.0"/>
</HBox.margin>
</CheckBox>
<CheckBox fx:id="cookiesCB" mnemonicParsing="false" text="CO">
<HBox.margin>
<Insets top="8.0"/>
</HBox.margin>
</CheckBox>
<CheckBox fx:id="cupcakesCB" mnemonicParsing="false" text="CC">
<HBox.margin>
<Insets top="8.0"/>
</HBox.margin>
</CheckBox>
<CheckBox fx:id="otherCB" mnemonicParsing="false" text="O">
<HBox.margin>
<Insets top="8.0"/>
</HBox.margin>
</CheckBox>
</children>
<padding>
<Insets left="18.0"/>
</padding>
</HBox>
<HBox>
<!--
included data here-->
<Pane id="selectionQuestions" />
</HBox>
<TextArea fx:id="notes" prefHeight="200.0" prefWidth="200.0"/>
<HBox alignment="BOTTOM_RIGHT" maxHeight="30.0" minHeight="30.0" prefHeight="100.0" prefWidth="200.0">
<children>
<Button id="submitOrderButton" mnemonicParsing="false" onAction="#submitOrder" text="submit"/>
</children>
</HBox>
</children>
</VBox>
</center>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
</BorderPane>
要在HBox中面積增加:check_box_selected。 FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Label?>
<HBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" minWidth="30" minHeight="30">
<Label text="Here's my selected checkbox label" />
</HBox>
FormController.scala:
package controller
import javafx.event.ActionEvent
import javafx.scene._
import javafx.stage.Stage
import javafx.scene.layout.Pane
import javafx.scene.control.CheckBox
import scalafxml.core.macros.sfxml
import scalafxml.core.{FXMLView, NoDependencyResolver}
import javafx.fxml.FXMLLoader
@sfxml
class FormController(private val selectionQuestions: Pane,
private val cakeCB: CheckBox,
private val cookiesCB: CheckBox,
private val cupcakesCB: CheckBox,
private val otherCB: CheckBox) {
def submitOrder(actionEvent: ActionEvent) {
val source = actionEvent.getSource.asInstanceOf[Node].getScene.getWindow.asInstanceOf[Stage]
source.hide;
}
def checkboxSelected(actionEvent: ActionEvent) {
val source = actionEvent.getSource
val box: CheckBox = source.asInstanceOf[CheckBox]
box.getId match {
case "cakeCB" => {
val resource = getClass.getResource("/scala/cake_box_selected.fxml")
val root = FXMLView(resource, NoDependencyResolver)
//This is the null pointer =>
selectionQuestions.getChildren.clear
selectionQuestions.getChildren.add(root)
}
}
}
}
實際上,有我用這些進口的理由。當我有「得」在控制器中的點擊來源,我用這個語法: VAL源= actionEvent.getSource VAL箱:複選框= source.asInstanceOf [複選框] 如果我使用scalafx導入,我得到一個說javafx.scene.control的ClassCastException。CheckBox不能轉換爲scalafx.scene.control.CheckBox。所以,到目前爲止,我一直在使用我的控制器中的javafx版本。是否有另一種方法可以讓我在我的控制器中使用scalafx版本,您知道嗎? –
我知道轉換異常是因爲FXML導入是javafx,如果我在我的控制器中使用scalafx,我會得到異常。 –
'@ sxml'也翻譯了事件處理參數,所以你可以在它們中使用scalafx的'ActionEvent'包裝器。不知道這是否解決了這個問題,我會盡力在以後重現您的問題。 – vigoo