弹出窗口标签未加载

时间:2014-03-13 15:29:23

标签: javafx-2

1.Trying以在弹出窗口中显示异常消息。异常消息未出现。

2.Eg:当我点击按钮时,弹出窗口(第二个fxml文件)将在标签中加载适当的异常消息

3.Popup窗口出现,但Label没有加载(Bold one - &gt; ExceptionLabel.setText(“请输入正确的文件路径”))它表示空指针异常。< / p>

4.我不确定我错过了什么。在FX中声明相同:ID以及与主控制器链接的第二个fxml文件。提前谢谢。

 @FXML
 public Label ExceptionLabel;
 Stage PopupWindow = new Stage();

 public void Buttonhandle(ActionEvent event) throws IOException {
    try {

        if(ESBOutboundFile!=null && OutputFile!=null){
         String Output = SBlogpaser.Logpaser(ESBInboundFile,ESBOutboundFile,OutputFile);
        System.out.println(Output);
        }else{
        Window(PopupWindow);
        **ExceptionLabel.setText("Please enter Proper file path");**

        }
    } catch (Exception ex) {
        System.out.println(ex);
    }

}

public void Window(Stage Popup) throws Exception {
   this.Popup=Popup;
   final FXMLLoader fxmlLoader = new FXMLLoader();
    Parent root= fxmlLoader.load(getClass().getResource("POPUPWindow.fxml"));            
    Scene scene1 = new Scene(root);
    Popup.setScene(scene1);
    Popup.show();      
}

Popup window without label

如果我将标签保留在“确定”手柄按钮中,它就会显示出来。

1 个答案:

答案 0 :(得分:1)

您希望ExceptionLabel在哪里实例化?

假设您将POPUPWindow.fxml文件的根目录的fx:controller属性指向当前类,它将只创建该类的新实例,并将值注入该实例。

表示当前实例中的字段ExceptionLabel

你可以通过将FXMLLoader的控制器设置为当前对象来完成这项工作,如下所示:

public void window(Stage popup) throws Exception {
   this.popup=popup; // why?
   final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("POPUPWindow.fxml"));
    fxmlLoader.setController(this);
    Parent root= fxmlLoader.load();            
    Scene scene1 = new Scene(root);
    popup.setScene(scene1);
    popup.show();      
}

然后从POPUPWindow.fxml中删除fx:controller属性。

但这似乎是一个非常糟糕的主意,因为现在当前对象充当两个不同FXML文件的控制器。这将充其量混淆,并且在相当合理的条件下会产生奇怪的结果。为弹出窗口编写不同的控制器类会好得多:

public class PopupController {
  private final String message ;
  @FXML
  private Label exceptionLabel ;

  public PopupController(String message) {
    this.message = message ;
  }

  public void initialize() {
    exceptionLabel.setText(message);
  }
}

然后使用上面的窗口(...)方法,但使用

fxmlLoader.setController(new PopupController("Please enter Proper file path"));

显然,如果您重复使用window(..)方法,您可能希望将该消息作为参数传递给该方法。

相关问题