Proposal for improving nested controller interaction in JavaFX 2.2

Greg Brown greg.x.brown at oracle.com
Wed Feb 22 06:11:01 PST 2012


Forgot to reply all earlier:

>> Being a devil's advocate the explicit use of a Map to store
>> controllers made me wonder ... what happens if I really want a a list of
>> controllers (say I want fallback controllers) or what if there's some sort
>> of conditional use controller?
> 
> The reason a Map is used to represent the includes is because it parallels the Map that is used as the document's namespace. The namespace is a key-value pair of all named items within the document. So, for example, when I assign an ID to an include tag, I can get access to the root element of the include's document by ID, but I can't get the document's controller:
> 
> <!-- "foo" points to whatever the root of foo.fxml is -->
> <fx:include fx:id="foo" source="foo.fxml"/>
> 
> Now, when we find an include with an ID, we'll also add the included document's controller (if any) to the including controller's include map, using the same ID as a key.

But now that I think about it, I wonder if it might make more sense to map nested controller instances directly to including controller fields, like we do with namespace values. For example, if my main document looks like this:

<VBox fx:controller="com.foo.MyMainController">
  …
  <fx:include fx:id="myInclude" source="my_include.fxml"/>
  …
</VBox>

My controller could look like this:

public class MyMainController extends Controller {
    @FXML private HBox myInclude;
    @FXML private MyIncludeController myIncludeController;

    …
}

When initialize() is called, myInclude would contain the root element from "my_include.fxml", and myIncludeController would contain the include's controller. That way, I wouldn't have to call getIncludes("myInclude") and cast the return type to MyIncludeController.

This would obviate the specific need for a base Controller class, but we might want to consider adding it anyways since doing so has some other benefits.

What do you think?

Greg



More information about the openjfx-dev mailing list