我有以下功能
public void loadWindowAndSendDataTest(String path, String appName, ConnectionData connectionData) {
try {
Stage subWindow = new Stage();
subWindow.initModality(Modality.APPLICATION_MODAL);
FXMLLoader loader = new FXMLLoader();
Parent parent = loader.load(getClass().getResource(path).openStream());
ExitController exitController = (ExitController) loader.getController();
exitController.getConnectionData(connectionData);
Scene scene = new Scene(parent);
subWindow.setScene(scene);
subWindow.setTitle(appName);
subWindow.show();
} catch(IOException e) {
e.printStackTrace();
}
我想实现的是具有更通用的功能,可以在其中传递类名(在本例中为ExitController),因此需要这样的代码:
public void loadWindowAndSendDataTest(String path, String appName, ConnectionData connectionData, String className) {
try {
Stage subWindow = new Stage();
subWindow.initModality(Modality.APPLICATION_MODAL);
FXMLLoader loader = new FXMLLoader();
Parent parent = loader.load(getClass().getResource(path).openStream());
/* do sth with className to obtain UsedClassController class */
UsedClassController usedClassController = (UsedClassController) loader.getController();
usedClassControler.getConnectionData(connectionData);
Scene scene = new Scene(parent);
subWindow.setScene(scene);
subWindow.setTitle(appName);
subWindow.show();
} catch(IOException e) {
e.printStackTrace();
}
假设是我传递给此函数的类已实现
getConnectionData()
功能。有没有办法做到这一点? 最佳答案
如果假设控制器类已实现getConnectionData(ConnectionData)
方法,则可以使用反射来调用该方法:
public void loadWindowAndSendDataTest(String path, String appName, ConnectionData connectionData) {
try {
Stage subWindow = new Stage();
subWindow.initModality(Modality.APPLICATION_MODAL);
FXMLLoader loader = new FXMLLoader();
Parent parent = loader.load(getClass().getResource(path).openStream());
Object controller = loader.getController();
Method getConnectionDataMethod =
controller.getClass().getMethod("getConnectionData", ConnectionData.class);
getConnectionDataMethod.invoke(controller, connectionData);
Scene scene = new Scene(parent);
subWindow.setScene(scene);
subWindow.setTitle(appName);
subWindow.show();
} catch(Exception e) {
e.printStackTrace();
}
}
这不是一个特别健壮或优雅的方法。也许更好的方法是使用
getConnectionData
方法定义一个接口:public interface ConnectionDataProvider {
public void getConnectionData(ConnectionData data) ;
}
并让您的控制器实现该方法:
public class ExitController implements ConnectionDataProvider {
public void getConnectionData(ConnectionData data) {
// ...
}
// existing code ...
}
然后,您可以假设控制器来自实现该方法的类:
public void loadWindowAndSendDataTest(String path, String appName, ConnectionData connectionData) {
try {
Stage subWindow = new Stage();
subWindow.initModality(Modality.APPLICATION_MODAL);
FXMLLoader loader = new FXMLLoader();
Parent parent = loader.load(getClass().getResource(path).openStream());
ConnectionDataProvider controller = loader.getController();
controller.getConnectionData(connectionData);
Scene scene = new Scene(parent);
subWindow.setScene(scene);
subWindow.setTitle(appName);
subWindow.show();
} catch(Exception e) {
e.printStackTrace();
}
}
请注意,这两种方法都不要求您传入控制器的类型(类)。如果由于某些原因确实需要这样做,则可以执行以下操作(使用接口方法):
public <T extends ConnectionDataProvider> void loadWindowAndSendDataTest(
String path, String appName,
ConnectionData connectionData, Class<T> controllerType) {
try {
Stage subWindow = new Stage();
subWindow.initModality(Modality.APPLICATION_MODAL);
FXMLLoader loader = new FXMLLoader();
Parent parent = loader.load(getClass().getResource(path).openStream());
T controller = loader.getController();
// if you wanted to cast explicitly here, you could do:
// T controller = controllerType.cast(loader.getController());
controller.getConnectionData(connectionData);
// do something with controllerType if you need....
Scene scene = new Scene(parent);
subWindow.setScene(scene);
subWindow.setTitle(appName);
subWindow.show();
} catch(Exception e) {
e.printStackTrace();
}
}
然后您可以使用
loadWindowAndSendDataTest("/path/to/fxml", "Application Name",
connectionData, ExitController.class);