JavaFX:ScrollPane方法getChildren()不可见 [英] JavaFX: ScrollPane method getChildren() is not visible

查看:329
本文介绍了JavaFX:ScrollPane方法getChildren()不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从AnchorPane和一些子元素创建了一个Stage(AnchorPane及其子元素已在Java Scene Builder中创建),层次结构如下所示:

I have created a Stage from an AnchorPane and some children elements (the AnchorPane and its children have been created in Java Scene Builder) and the hierarchy is shown below:

舞台和场景当然是在程序初始化时以编程方式创建的。
我还想以编程方式添加GridPane作为图像中显示的ScrollPane的子项。
在我的程序(特定窗口的控制器)中,我可以获得对ScrollPane的引用:

The stage and the scene are of course created programmatically at program initialization. I want to also add programmatically a GridPane as a child of the ScrollPane shown in the image. In my program (the controller of the specific window) I can get a reference to the ScrollPane:

@FXML
private ScrollPane srcPaneUsers;

(id srcPaneUsers已通过Scene Builder属性窗口的相应字段给出)

(the id srcPaneUsers has been given through the respective field of the properties windows of Scene Builder)

但是!!当我尝试以编程方式添加在运行时通过控制器初始化方法的以下行创建的新GridPane:

HOWEVER!!: When I try to add programmatically a new GridPane created at run-time through the following lines of the initialize method of the controller:

public void initialize(URL location, ResourceBundle resources) {
    myGridPane = new GridPane();
    srcPaneUsers.getChildren().add(myGridPane);
}

我收到一个编译时错误,上面写着
来自Parent类型的getChildren()方法不可见。
所以我不能在运行时添加我的GridPane。
有什么想法吗?

I get a compile time error that says "The method getChildren() from the type Parent is not visible." And so I cannot add my GridPane at run time. Any ideas?

推荐答案

你正在调用错误的方法。你需要

You are calling the wrong method. You need

srcPaneUsers.setContent(myGridPane);

getChildren()方法在 Parent 是受保护的方法。它在 Pane 中被覆盖为公共方法,因此对于布局窗格( GridPane BorderPane 等)你可以直接操作子节点列表。

The getChildren() method defined in Parent is a protected method. It is overridden in Pane to be a public method, so for layout panes (GridPane, BorderPane, etc) you can directly manipulate the list of child nodes.

ScrollPane 的层次结构是 ScrollPane extends Control extends Region extends Parent ,所以它继承了 protected getChildren()方法。这实际上是有道理的: ScrollPane 的子节点就像视口(剪辑内容的视图)和滚动条:你真的不想要用户操纵那些,否则你最终可能不再正常运作。 ScrollPane s实际上只有一个可配置的节点: ScrollPane 提供视图的节点:这是称为内容,并通过 contentProperty() getContent() setContent()方法。

ScrollPane's hierarchy is ScrollPane extends Control extends Region extends Parent, so it inherits the protected getChildren() method. And this actually makes sense: the child nodes of a ScrollPane are things like the viewport (which clips the view of the content) and scrollbars: you don't really want the user to be manipulating those, otherwise what you end up with is likely not to function correctly any more. ScrollPanes really only have one node that is configurable: the node of which the ScrollPane is providing a view: this is referred to as its content and accessed via the contentProperty(), getContent(), and setContent() methods.

其他容器式控件的工作方式类似。 TabPane 类公开一个 getTabs()方法,该方法返回 ObservableList< Tab> ;并且 Tab 类依次公开 contentProperty(),以便您可以访问每个单独选项卡的内容。 SplitPane 选项卡公开 getItems()方法,返回 ObservableList< Node> 用于窗格中显示的节点。在 SplitPane 的情况下,它的子节点不仅仅是项目:它们包括代表分隔符的可视组件等。在这两种情况下, getChildren() method是受保护的方法(继承自 Parent )。

Other "container-like controls" work similarly. The TabPane class exposes a getTabs() method that returns an ObservableList<Tab>; and the Tab class in turn exposes a contentProperty() so you can access the content of each individual tab. The SplitPane tab exposes a getItems() method returning an ObservableList<Node> for the nodes displayed in the pane. In the case of a SplitPane its child nodes are more than the items: they include the visual components representing the dividers, etc. In both these cases, the getChildren() method is a protected method (inherited from Parent).

请注意, Parent 还定义了一个公共 getChildrenUnmodifiable()方法,该方法返回子节点列表的不可修改视图。您可以使用它来检查任何父级的节点层次结构,但不能对其进行修改。 (但是出于调试目的,特别是对于使用CSS,我建议使用 ScenicView 。)

Note that Parent also defines a public getChildrenUnmodifiable() method, which returns an unmodifiable view of the list of child nodes. You can use this to examine the node hierarchy of any parent, but not modify it. (For debugging purposes, however, especially for working with CSS, I recommend using ScenicView.)

这篇关于JavaFX:ScrollPane方法getChildren()不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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