-2

I want to create something like scenebuilder for worksheet documents and was wondering how i could allow my users to use custom components.

For that i would like to import fxml files that my users created and render the controls defined in them in my scene. Is there a way to load arbitrary fxml snippets as controls/views assuming they are correct fxml?

so i would write my app with some components coming with it and people could select fxml files from their file system to add it to their worksheet (something like a sudoku fram maybe or notesheet lines for example)

What would the code look like to import random fxml snippets into a view (lets say a simple pane) and have them rendered properly? What prerequisites would the fxml have to fullfill for it to work?

Daniel
  • 13
  • 6
  • 1
    Scenebuilder provides an open source kit which allows easier integration of some of its logic into other applications, you could take a look at the `kit` directory in the [scene builder source](https://github.com/gluonhq/scenebuilder) to see how it performs the tasks you are interested in, but, depending upon what you want to do, it may or may not be overkill for the functionality you require. – jewelsea Sep 03 '19 at 22:37
  • @jewelsea that would integrate the whole of scenebuilder into my project. i only want to turn fxml snippets into views that i can place on/in a parent container like a pane – Daniel Sep 04 '19 at 06:29
  • Not sure how useful that would be. A window form without a controller (or at least a non-trivial controller) wouldn't do anything. You'd probably be better off having a far simpler xml describing a simple data request form and take that as input. – Neil Sep 04 '19 at 06:31
  • it wouldn't necessarily be interactive components, since most of the functionality revolves around printing the scene to paper for student worksheets. – Daniel Sep 04 '19 at 06:36
  • This question should be re-opened. With the current edits that the question has in place, it is not too broad. Specifically the question: "What would the code look like to import random fxml snippets into a view (lets say a simple pane) and have them rendered properly? What prerequisites would the fxml have to fullfill for it to work?" is very specific and applicable to a StackOverflow question. The answer requires minimal code to demonstrate the first part and the answer to the second part of the question (on pre-requisites for it to work) may be quite useful to others. – jewelsea Sep 04 '19 at 21:59

1 Answers1

1

Have a look in the documentation of FXMLLoader. You can load a FXML-File using the load(InputStream) Method.

An InputStream can be anything - a resource in your classpath, a file on your local FileSystem, a http(s) resource, ...

Felix
  • 2,256
  • 2
  • 15
  • 35
  • but doesn't it have to have a controller attached? – Daniel Sep 04 '19 at 06:19
  • 1
    @Daniel I ran some tests. If you have any [controller method event handlers](https://docs.oracle.com/javafx/2/api/javafx/fxml/doc-files/introduction_to_fxml.html#controller_method_event_handlers) defined in your FXML, such as `onAction="#buttonAction"`, then you need an associated controller with the event handlers defined on it. If you don't define any event handlers like this in your FXML, then the FXMLLoader can load the FXML without any associated controller (just don't define a `fx:controller="com.foo.MyController"` element in your FXML). – jewelsea Sep 04 '19 at 17:41