2016-05-30 104 views
0

我正在为家庭成员创建订单管理系统。我需要应用程序能够在运行时自行更新,当用户选择是否为蛋糕,饼干,蛋糕或其他。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) 
     } 
    } 
    } 

} 

回答

1

您必须使用ScalaFX VERS控制器类的构造函数中的控件的离子。所以,简单地代替

import javafx.scene.layout.Pane 
import javafx.scene.control.CheckBox 

使用

import scalafx.scene.layout.Pane 
import scalafx.scene.control.CheckBox 

这是一个应该被固定在scalafxml库的限制。 (创建一个问题吧:https://github.com/vigoo/scalafxml/issues/19

EDITED

正如你在评论中解释,使用ScalaFX类型时,尝试使用ActionEvent的来源,当它失败,转换异常。此属性的类型为Object,并持有对JavaFX控件本身的引用,该类型的类型为javafx.scene.control.CheckBox。由于宏的限制,这可以隐式转换为您必须使用的ScalaFX包装器。

所以解决的办法是:

  • 到处使用
  • 的ScalaFX类型除了中投,在那里你必须用JavaFX类型 - 编译器将转换的照顾。

例子:

import scalafx.event.ActionEvent 
import scalafx.scene.control.{CheckBox, ComboBox, TextField} 

// ... 

    def testCheckBoxAction(event: ActionEvent): Unit = { 
    val source: CheckBox = event.source.asInstanceOf[javafx.scene.control.CheckBox] 
    // ... 
    } 
+0

实际上,有我用这些进口的理由。当我有“得”在控制器中的点击来源,我用这个语法: VAL源= actionEvent.getSource VAL箱:复选框= source.asInstanceOf [复选框] 如果我使用scalafx导入,我得到一个说javafx.scene.control的ClassCastException。CheckBox不能转换为scalafx.scene.control.CheckBox。所以,到目前为止,我一直在使用我的控制器中的javafx版本。是否有另一种方法可以让我在我的控制器中使用scalafx版本,您知道吗? –

+0

我知道转换异常是因为FXML导入是javafx,如果我在我的控制器中使用scalafx,我会得到异常。 –

+0

'@ sxml'也翻译了事件处理参数,所以你可以在它们中使用scalafx的'ActionEvent'包装器。不知道这是否解决了这个问题,我会尽力在以后重现您的问题。 – vigoo

相关问题