This block of code uses the Reflections project to pull in a bunch of files marked up with my custom @SubApplication annotation.
Reflections reflections = new Reflections(new ConfigurationBuilder() .setUrls(ClasspathHelper.forPackage("fms")) .setScanners(new TypeAnnotationsScanner())); Set<Class<?>> subapps = reflections.getTypesAnnotatedWith(SubApplication.class); subapps.stream().sorted(Comparator.comparing(Class::getName)).forEach(sa -> { SubApplication saAnnotation = sa.getAnnotation(SubApplication.class); Button btn = new Button(saAnnotation.buttonTitle()); btn.setUserData(sa); // holds the metadata btn.setOnAction(evt -> startScreen(evt)); vbox.getChildren().add(btn); });
getTypesAnnotatedWith() returns a Set of Classes which is the basis for my processing.
I stream the Set into a sorted() stage. Comparator.comparing is a static method that accepts the shorthand Class:getName. This comparison will sort the classes by class name. (the Reflections call doesn't return an alphabetically sorted list.) The sorted list is funneled into a button-creating block.
By the time that the add to the VBox is made, the input is sorted and will appear in the proper order on the Stage.
No comments:
Post a Comment