【发布时间】:2015-12-05 20:05:15
【问题描述】:
这是我的第一个 FXML 应用程序。以前我对 Swing 已经很熟悉了。我只是想从舞台上的控件中保存几个值,以便用户输入的值在运行时重新加载。 ReliableOneShotCloseHandler 是我 posted a while ago here 的 JavaFX 版本。然而,当 windowClosing 函数被调用时,控件似乎已经被处理掉了......?我在指示的行收到 NullPointerException:
package testfxmlloader;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.control.Label;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
/**
*
* @author maskedcoder
*/
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private AnchorPane apMain;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
ReliableOneShotCloseHandler roscSaveSettings = new ReliableOneShotCloseHandler(apMain.getScene().getWindow(), new ReliableOneShotCloseHandler.CloseDuties() {
@Override
public boolean confirmClose(WindowEvent e) {
return true;
}
@Override
public void windowClosing(WindowEvent e) {
saveSettings();
}
});
}
public void saveSettings() {
System.out.println("save setting: " + label.getText()); // gets NullPointerException because label is null.
}
}
class ReliableOneShotCloseHandler {
public interface CloseDuties {
public boolean confirmClose(WindowEvent e);
public void windowClosing(WindowEvent e);
}
private final CloseDuties closeDuties;
private boolean windowClosingFired = false;
private final EventHandler<WindowEvent> openEvent;
private final EventHandler<WindowEvent> closeEvent;
public ReliableOneShotCloseHandler(Window thisWindow, CloseDuties iniCloseDuties) {
super();
closeDuties = iniCloseDuties;
openEvent = (WindowEvent event) -> {
windowClosingFired = false;
};
closeEvent = (WindowEvent event) -> {
if(!windowClosingFired) {
if(closeDuties.confirmClose(event)) {
closeDuties.windowClosing(event);
windowClosingFired = true;
}
else
event.consume();
}
};
thisWindow.setOnShowing(openEvent);
thisWindow.setOnShown(openEvent);
thisWindow.setOnCloseRequest(closeEvent);
thisWindow.setOnHiding(closeEvent);
thisWindow.setOnHidden(closeEvent);
}
}
public class TestFXMLLoader extends Application {
FXMLLoader fxmll;
FXMLDocumentController fdc;
@Override
public void start(Stage stage) throws Exception {
fdc = new FXMLDocumentController();
fxmll = new FXMLLoader();
fxmll.setBuilderFactory(new JavaFXBuilderFactory());
Parent root = fxmll.load(getClass().getResource("FXMLDocument.fxml"));
fxmll.setController(fdc);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
这是 FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" fx:id="apMain" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="testfxmlloader.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
</children>
</AnchorPane>
应该在获得关闭权限之前调用windowClosing 函数,因此所有控件都应该正常工作。上面的测试代码基于 NetBeans 的默认 JavaFXML 应用程序,只是添加了 ReliableOneShotCloseHandler 和伴随代码。我的逻辑有问题吗?有没有其他方法可以做到这一点?
更新:包含了一些不应该包含的代码行。它们已被删除。
【问题讨论】: