2014-03-13 81 views
0

1.尝试在弹出窗口中显示异常消息。异常消息未显示。弹出窗口标签未加载

2.Eg:当我点击按钮的弹出窗口(二FXML文件)是在标签

3.Popup窗口出现在用正确的异常信息加载,但标签没有加载(加粗一个 - >ExceptionLabel.setText(“请输入正确的文件路径”))它说空指针异常。

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

如果我保持标签中得到显示为 “OK” 按钮,手柄。

回答

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();  
} 

,然后取下FX:从POPUPWindow.fxml控制器属性。

虽然这似乎是一个非常糟糕的主意,因为现在当前对象充当两个不同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")); 

很显然,如果你重用窗口(..)方法,您可能想要将该消息作为参数传递给该方法。

+0

仍然无法正常工作....... – mani

+0

您使用了PopupController方法,并且exceptionLabel仍然为空? –

+0

这次更好没有例外,但消息没有得到显示..代码正确调用popupcontroller,但它没有传递给公共无效initialize()方法。如果手动调用它将引发标签设置文本的空指针异常 – mani