在同一场景中加载新的fxml [英] Loading new fxml in the same scene

查看:117
本文介绍了在同一场景中加载新的fxml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个fxml文件:




  • 布局(标题,菜单栏和内容)

  • Anchorpane(它应该放在其他fxml文件的内容中)



我想知道如何加载第二个来自主场景的内容空间内的文件。在javaFX中工作是一件好事还是加载一个新场景更好?



我正在尝试这样做,但它并没有工作:

  @FXML 
私人AnchorPane内容;

@FXML
private void handleButtonAction(ActionEvent event){
content =(AnchorPane)FXMLLoader.load(vista2.fxml);
}

感谢您的帮助。

解决方案

为什么你的代码不起作用



加载程序创建一个新的AnchorPane,但是您永远不会将新窗格添加到场景图中的父级。



快速修复



而不是:

  content =(AnchorPane)FXMLLoader.load(vista2.fxml); 

写:

  content.getChildren()SETALL(FXMLLoader.load( vista2.fxml))。 

用新视图替换内容儿童。内容本身保留在场景图中,因此当您设置它的子项时,您也会同时将它们附加到场景图。



您可能需要玩围绕布局(例如使用自动调整大小布局,如StackPanes而不是AnchorPanes)来获得您想要的确切行为。



我建议不要只采用快速修复。审查下面链接的简单框架,因为它可能为您提供更通用的机制来获得您想要的行为。



参考FXML导航框架



我创建了一个更广泛和更好的支持。



寻找更简单的东西?



只需换出场景根:在JavaFX中更改场景



其他选项?



动画过渡和其他:切换JavaFX中的窗格


I have 2 fxml files:

  • Layout (header, menubars and content)
  • Anchorpane (it's supposed to be placed inside the content from the other fxml file)

I would like to know how can I load the second file inside the content space from the "Master" scene. And is that a good thing to do working in javaFX or is it better to load a new scene?

I'm trying to do something like this, but it doesn't work:

@FXML
private AnchorPane content;

@FXML
private void handleButtonAction(ActionEvent event) {        
    content = (AnchorPane) FXMLLoader.load("vista2.fxml");
}

Thanks for the help.

解决方案

Why your code does not work

The loader creates a new AnchorPane, but you never add the new pane to a parent in the scene graph.

Quick Fix

Instead of:

content = (AnchorPane) FXMLLoader.load("vista2.fxml");

Write:

content.getChildren().setAll(FXMLLoader.load("vista2.fxml"));

Replacing the content children with your new vista. The content itself remains in the scene graph, so when you set it's children, you are also attaching them to the scene graph at the same time.

You might need to play around with layout (e.g. work with auto resizing layouts like StackPanes rather than AnchorPanes) to get the exact behaviour you want.

Rather than just adopting the quick fix, I would advise reviewing the simple framework linked below as that might provide you with a more general purpose mechanism to get the behaviour you want.

Reference FXML Navigation Framework

I created a small framework for swapping fxml controlled content panes in and out of a portion of the main scene.

The mechanism of the framework is the same as suggested in kithril's answer.

  1. A main pane for the outer fxml acts as a holder for child panes.
  2. The main controller for the outer fxml supplies a public method that can be used to swap the child panes.
  3. A convenience navigator class is statically initialized with the main controller for the outer layout.
  4. The navigator provides a public static method to load a new child pane into the main container (by invoking a method on the main controller).
  5. Child panes are generated in the navigator by their respective fxml loaders.

Why a Framework

The framework seems like overkill for answering your question, and perhaps it is. However, I have found that the two most asked topic related to FXML are:

  1. Navigation between panes generated by FXML (this question).
  2. How to pass data between FXML controllers.

So I felt that a small demo framework was warranted for this case.

Sample Framework Output

The first screen shows the application layout displaying the first vista. The contents are a header which is defined in the main application layout and an aliceblue colored interchangable child content pane.

In the next screen, the user has navigated to the second vista, which retains the constant header from the main layout and replaces the original child pane with a new coral colored child content pane. The new child has been loaded from a new fxml file.

Looking for Something More Substantial?

A lightweight framework which is more extensive and better supported than the sample framework from this question is afterburner.fx.

Looking for Something Even Simpler?

Just swap out the scene root: Changing Scenes in JavaFX.

Other Options?

Animated Transitions and others: Switch between panes in JavaFX

这篇关于在同一场景中加载新的fxml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆