This discussion is archived
5 Replies Latest reply: Mar 4, 2013 4:53 AM by James_D RSS

Controls FXML

895675 Newbie
Currently Being Moderated
How do I create and read at runtime codes fxml?

Thanks
  • 1. Re: Controls FXML
    James_D Guru
    Currently Being Moderated
    You probably need to provide a bit more information as to what you are trying to do in order to get your question answered.

    In short, to read FXML, you can use an FXMLLoader: see example 4-1 in the [url http://docs.oracle.com/javafx/2/fxml_get_started/fxml_tutorial_intermediate.htm]FXML tutorial, for example.

    To write an FXML file programmatically, you can use standard Java IO techniques to generate the file. There's a [url http://docs.oracle.com/javase/tutorial/essential/io/index.html]tutorial for that too, but not in the context of FXML.

    I can't really see why you'd want to do this, though. If your aim is just to programmatically generate a UI, then it would be easier to just build the scene graph with Java code, instead of FXML.
  • 2. Re: Controls FXML
    895675 Newbie
    Currently Being Moderated
    I need to get information from the database and create the screen, for example: create multiple labels, as the return of the query from the database. I want to do in fxml because it is faster

    Thanks
  • 3. Re: Controls FXML
    e-dubkendo Newbie
    Currently Being Moderated
    I think the approach you want is something like the MVC pattern, where you have a Model of the data which the Controller uses to pull data from the database and populate the View (the fxml) at runtime. You should take a look at this tutorial: http://edu.makery.ch/blog/2012/11/16/javafx-tutorial-addressapp-1/ . It's very thorough and should give you a good example of what I mean.

    He gets his data from an ObservableList instead of a database, but there is no reason it couldn't have been a database instead.
  • 4. Re: Controls FXML
    895675 Newbie
    Currently Being Moderated
    I need to make a query in the database, the return of this consultation, will determine how many objects I have to create in the fxml. For example, the query returns 20 records, so I need to create 20 labels to store these records. I can not let the labels created in fxml, because I do not know how many records I will return.

    Thanks
  • 5. Re: Controls FXML
    James_D Guru
    Currently Being Moderated
    You need to do that part in the controller. Use the FXML to define the structure of the user interface, and use the controller to populate it with data (and respond to user input).

    Usually people use TableView or ListView to display database-type results, in which case you can just call setItems(...) on the table or list and pass in a list of items of arbitrary length. But there's nothing to stop you using a bunch of labels in a VBox (or something) if you prefer:

    Example.fxml
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.layout.BorderPane?>
    <?import javafx.scene.layout.VBox?>
    
    <!-- Any structure you need -->
    <BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="ExampleController">
    <!-- ... -->
         <center>
               <VBox fx:id="dataDisplay" />
             </center>
    <!-- ... -->
    </BorderPane>
    ExampleController.java
    import javafx.fxml.FXML ;
    import javafx.scene.layout.VBox ;
    import javafx.scene.control.Label ;
    
    import java.util.List ;
    
    public class ExampleController {
      @FXML private VBox dataDisplay ;
    
      public void initialize() {
        List<String> data = // ... get data from database
        for (String value : data) {
          dataDisplay.getChildren().add(new Label(value));
        }
      }
    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points