如何将参数传递给javafx中的辅助窗口?有没有办法与相应的控制器通信?
例如:用户从a中选择一个客户,TableView并打开一个新窗口,显示客户的信息.
Stage newStage = new Stage();
try
{
AnchorPane page = (AnchorPane) FXMLLoader.load(HectorGestion.class.getResource(fxmlResource));
Scene scene = new Scene(page);
newStage.setScene(scene);
newStage.setTitle(windowTitle);
newStage.setResizable(isResizable);
if(showRightAway)
{
newStage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
newStage将是新窗口.问题是,我找不到告诉控制器在哪里查找客户信息的方法(通过传递id作为参数).
有任何想法吗?
parameters dependency-injection javafx parameter-passing fxml
各位晚上好,
我已经在这个主题上发现了很多帖子,但我仍然无法将一个对象从Controller1传递给Controller2.有没有一个完整的教程或一些示例项目,这样做?
在我遇到困难之前,我已经走到了这一步:
国家级
public class Country {
private SimpleStringProperty country = new SimpleStringProperty("");
//Constructor
public Country() {
}
//GETTERS
public String getCountry() {
return country.get();
}
//SETTERS
public void setCountry(String value) {
country.set(value);
}
@Override
public String toString() {
return getCountry();
}
}
Run Code Online (Sandbox Code Playgroud)
程序启动时,主要的FXML被加载(Sample.fxml).其中包含一个边框窗格,顶部面板中有一个菜单栏,中间有一个内容窗格.在初始化时,我创建一个新的Country对象并将其存储在全局变量中.我有一个方法,当单击一个菜单项时,将另一个FXML加载到内容窗格中:
SampleController.java
public class SampleController implements Initializable {
@FXML
private Pane pContent;
private Country c;
@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
System.out.println(c); //this prints Belgium, which is correct
URL url = getClass().getResource("Sub1.fxml");
FXMLLoader fxmlloader …Run Code Online (Sandbox Code Playgroud) 我有一个登录屏幕,我想将登录ID从LoginController传递给MainController,所以我可以访问一些函数来更改密码等等.
我像这样加载控制器:
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxml/Main.fxml"));
Parent root = (Parent)fxmlLoader.load();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
Run Code Online (Sandbox Code Playgroud)
Main.fxml受限于MainController.java.有没有办法可以传递我需要的用户ID,并在控制器的initialize()方法上访问它?
考虑以下样本.
如何访问控制器中应用程序的参数/参数?
谢谢.
注意:我曾尝试将App.java和MyController.java混合在一个Class文件中,但没有帮助.
App.java(简化):
public class App extends Application {
public static void main(String[] args) {
Application.launch(App.class, args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// output arguments in console
System.out.println(getParameters().getNamed().toString());
Parent root = FXMLLoader.load(getClass().getResource("MyView.fxml"));
final Scene scene = new javafx.scene.Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
MyController.java(简化):
public class MyController implements Initializable {
@Override
public void initialize(URL url, ResourceBundle rb) {
// HOW TO getParameters() HERE ?
}
@FXML
private Button myButton;
@FXML
private void my_Action(ActionEvent event) { …Run Code Online (Sandbox Code Playgroud) 在我的Guice模块中,我想关联FXML文件和它们的控制器,目前它看起来像这样:
public class GuiceModule extends AbstractModule
{
@Override
protected void configure()
{
// associate controllers and fxml files
bind(MainController.class).toInstance((MainController)loadController("/main.fxml"));
bind(SubController.class).toInstance((SubController)loadController("/content.fxml"));
}
protected Object loadController(String url)
{
InputStream fxmlStream = null;
try
{
fxmlStream = getClass().getResourceAsStream(url);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(url));
loader.setControllerFactory(new Callback<Class<?>, Object>() {
public Object call(Class<?> clazz) { // clazz because class is a reserved word
return injector.getInstance(clazz); // PROBLEM: no access to the injector here
}
});
loader.load(fxmlStream);
return loader.getController();
}
// [..] exception handling
}
}**strong …Run Code Online (Sandbox Code Playgroud) 在Spring框架中,我可以使用配置文件来加载类的成员变量.有没有办法在javafx中使用自定义控制器或自定义对象执行此操作?