5

我想在FXML显示之前更改弹出窗口中的标签和文本。这样做是因为所有的帮助项目都非常相似,我想制作一个模板FXML,然后相应地更改标签和文本区域的值以减少编码。

我很惊讶我没有遇到模板方法JavaFX。如果有更好的方法来解决这个问题,请提出建议。

对于手头的任务,我有以下内容:

@FXML
private void documentationAction(ActionEvent action) throws IOException {
    Stage dialogue = new Stage();
    Parent root = null;
    FXMLLoader loader = new FXMLLoader();
    root = loader.load(getClass().getResource("dialogues/dialogue.fxml"));
    Scene scene = new Scene(root);
    /*
     * change text values in title (Label) and text (TextArea)
     * and change the behavior of the findMore button
     */
    dialogue.setTitle("Documentation");
    dialogue.setScene(scene);
    dialogue.show();
}

我希望能够更改FXML 文件中的变量,我为此getterssetters控制器类中创建了这些变量,但我不知道如何做到这一点。

有办法吗?似乎有必要在多窗口应用程序中的窗口之间更改信息,所以我确信我缺少一些简单的东西。

控制器的更新代码:

public class DialogueController implements Initializable {

    @FXML
    private Button more;
    @FXML
    private Button close;
    @FXML
    private TextArea text;
    @FXML
    private Label title;

    public TextArea getText() {
        return text;
    }

    public void setText(String text) {
        this.text.setText(text);
    }

    public Label getTitle() {
        return title;
    }

    public void setTitle(String text) {
        this.title.setText(text);
    }

    @FXML
    public void closeAction(ActionEvent action) {
        // get a handle to the stage
        Stage stage = (Stage) close.getScene().getWindow();
        // do what you have to do
        stage.close();
    }

    @FXML
    public void moreAction(ActionEvent action) {
    }

    @FXML
    public void makeDocumentation(ActionEvent action) {
        this.title.setText("Documentation");
    }

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }
}

全对话 FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" padding="$x1" prefHeight="500.0" prefWidth="400.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml" fx:controller="pdbpro.dialogues.DialogueController">
  <children>
    <GridPane alignment="CENTER" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <children>
        <Label fx:id="title" alignment="CENTER_LEFT" prefWidth="-1.0" styleClass="title" text="Title" underline="false" visible="true" GridPane.columnIndex="0" GridPane.rowIndex="0">
          <GridPane.margin>
            <Insets fx:id="x1" />
          </GridPane.margin>
        </Label>
        <TextArea fx:id="text" editable="false" focusTraversable="false" prefWidth="200.0" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="1">
          <GridPane.margin>
            <Insets bottom="24.0" left="15.0" right="15.0" />
          </GridPane.margin>
        </TextArea>
        <ToolBar GridPane.columnIndex="0" GridPane.rowIndex="2">
          <items>
            <Button fx:id="more" mnemonicParsing="false" text="Find more" />
            <Button fx:id="close" mnemonicParsing="false" onAction="#closeAction" text="Close" />
          </items>
          <GridPane.margin>
            <Insets bottom="14.0" />
          </GridPane.margin>
        </ToolBar>
      </children>
      <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      </columnConstraints>
      <padding>
        <Insets fx:id="x1" />
      </padding>
      <rowConstraints>
        <RowConstraints maxHeight="60.0" minHeight="60.0" prefHeight="60.0" valignment="CENTER" vgrow="NEVER" />
        <RowConstraints maxHeight="1.7976931348623157E308" minHeight="50.0" prefHeight="0.0" vgrow="ALWAYS" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      </rowConstraints>
    </GridPane>
  </children>
  <stylesheets>
    <URL value="@dialogue.css" />
  </stylesheets>
</AnchorPane>

这些行的完整堆栈跟踪错误

FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(getClass().getResource("dialogues/dialogue.fxml"));
DialogueController dc = loader.getController();
dc.makeDocumentation(); //ERROR
Scene scene = new Scene(root);


java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1440)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
    at javafx.event.Event.fireEvent(Event.java:171)
    at javafx.scene.control.MenuItem.fire(MenuItem.java:456)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1188)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$6.handle(ContextMenuContent.java:1139)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$6.handle(ContextMenuContent.java:1137)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
    at javafx.event.Event.fireEvent(Event.java:171)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3324)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3164)
    at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3119)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1559)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2261)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:228)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
    at com.sun.glass.ui.View.notifyMouse(View.java:922)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1435)
    ... 37 more
Caused by: java.lang.NullPointerException
    at pdbpro.MainController.documentationAction(MainController.java:82)
    ... 42 more
4

3 回答 3

5

我可以看到的一种方法是让您的控制器实现可初始化并实现初始化方法。当控制器被 fxml 加载器创建时,initialize 方法将被自动调用。当调用 initialize 方法时,所有声明的 fxml 对象都应该被初始化并准备好进行定制。

[编辑] 在阅读了initializable 的文档后,它说如果创建了一个没有参数的名为 initialize 的方法,它将被 fxml 加载器自动调用。因此,据我了解,您不需要实现可初始化。只需创建一个没有参数的初始化方法。

如果需要,可以在fxml 的介绍中找到更多信息。

于 2012-12-24T08:31:19.463 回答
3

尝试这个:

root = (Parent) loader.load(getClass().getResource("dialogues/dialogue.fxml").openStream());
DialogueController dc = loader.getController();
dc.setTitle("title");
dc.setTextArea("text");
// and so on..
...
dialogue.show();

断言 DialogueController dc 不为空。并假设“标题”是 fxml 文件中的标签fx:id=titleLabel,它应该在您的控制器类中得到支持,如下所示:

...
@FXML private Label titleLabel;
...
public void setTitle(String txt) {
    titleLabel.setText(txt);
}
...
于 2012-12-24T08:28:00.650 回答
1

我建议您将所有 FXML 加载代码隐藏到控制器。为此,可以扩展 Stage 类:

控制器:

public class DialogueController extends Stage implements Initializable{
    @FXML private Button more;
    @FXML private Button close;
    @FXML private TextArea text;
    @FXML private Label title;

    public DialogueController(Parent parent){
        FXMLLoader loader = new FXMLLoader(getClass().getResource("dialogues/dialogue.fxml"));
        fxmlLoader.setController(this);
        super.setTitle("Documentation");
        try{
            setScene(new Scene((Parent) loader.load()));
        }catch(IOException e){
            e.printStackTrace();        
        }
        /*
         * Change values here
         */     
    }   
}

调用你的控制器:

@FXML
private void documantationAction(ActionEvent action) throws IOException{
    DialogueController controller = new DialogueController(null);
    controller.show();
}
于 2015-10-20T08:40:29.470 回答