1

I am making a GUI in Java where I am getting the data I need through SPARQL-queries. The program is about exercises, and I would like to make a container (hbox/vbox) for each exercise with relevant information. The problem is that right now, the program looks really ugly, because I am generating the hbox with pure javafx with the code below:

ResultSet result = Querying.ontologyQuery(Strings.getFeatured());
    List<Resource> exerciseList = new ArrayList<>();
    List<Resource> mainMuscleList = new ArrayList<>();
    List<Literal> equipmentList = new ArrayList<>();
    while (result.hasNext()) {
        QuerySolution qs = result.next();
        Resource exercise = qs.getResource("Exercise");
        Resource mainMuscle = qs.getResource("mainMuscle");
        Literal equipment = qs.getLiteral("Equipment");
        exerciseList.add(exercise);
        mainMuscleList.add(mainMuscle);
        equipmentList.add(equipment);
    }

    for (int i = 0; i < 4; i++) {
        HBox hbox = addHbox();
        vbox.getChildren().add(hbox);
        Label exercise = new Label();
        Label mainMuscle = new Label();
        Label equipment = new Label();

        mainMuscle.setText(mainMuscleList.get(i).getLocalName());
        equipment.setText(equipmentList.get(i).getString());
        exercise.setText(exerciseList.get(i).getLocalName());

        hbox.getChildren().add(exercise);
        hbox.getChildren().add(mainMuscle);
        hbox.getChildren().add(equipment);

I guess what I am asking, is it if it's possible to make a template sort of in fxml, and just deploy the information from the SPARQL-queries into the placeholders in the "template". Any help is appriciated on how to go about this

JokkeMedKniven
  • 81
  • 1
  • 11

1 Answers1

1

You can use the custom component FXML pattern (also see this question).

Basically, you can do

<?xml ... ?>
<?import javafx.scene.layout.HBox ?>
<?import javafx.scene.control.Label ?>

<fx:root type="HBox">
    <Label fx:id="exerciseLabel"/>
    <Label fx:id="mainMuscleLabel"/>
    <Label fx:id="equipmentLabel"/>
</fx:root>

and then define a controller/component class:

public class ExerciseView extends HBox {

    @FXML
    private Label exerciseLabel ;
    @FXML
    private Label mainMuscleLabel ;
    @FXML
    private Label equipmentLabel ;

    public ExerciseView(Resource exercise, Resource mainMuscle, Literal equipment) {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Exercise.fxml"));
        loader.setController(this);
        loader.setRoot(this);
        try {
            loader.load();
            mainMuscleLabel.setText(mainMuscle.getLocalName());
            equipmentLabel.setText(equipment.getString());
            exerciseLabel.setText(exercise.getLocalName());
        } catch (IOException exc) {
            throw new RuntimeException(exc);
        }
    }

    // ...
}

And then of course:

ResultSet result = Querying.ontologyQuery(Strings.getFeatured());
while (result.hasNext()) {
    QuerySolution qs = result.next();
    Resource exercise = qs.getResource("Exercise");
    Resource mainMuscle = qs.getResource("mainMuscle");
    Literal equipment = qs.getLiteral("Equipment");
    vbox.getChildren().add(new ExerciseView(exercise, mainMuscle, equipment));
}

(or some simple modification of that, if you don't want your database code to know about your UI...).

James_D
  • 201,275
  • 16
  • 291
  • 322