I'm trying to populate multiple MenuButton's in javaFX with an arrayList of checkMenuItems. To add the check menu items im doing:
myMenuButton.getItems().addAll(ChecKMenuItemList);
this is what my code looks like
class Scratch {
private void updateClassList(ArrayList<Class> classArrayList) {
ArrayList<String> classCodeList = new ArrayList<>();
ArrayList<CheckMenuItem> checkMenuItemList = new ArrayList<>();
ArrayList<CheckMenuItem> checkMenuItemList2 = new ArrayList<>();
ArrayList<String> classNameList = new ArrayList<>();
//Create Arrays of class elements
for(Class aClass : classArrayList){
checkMenuItemList.add(new CheckMenuItem(aClass.getClassCode()));
}
//Clear Class Lists
addStudentsToClassClassListView.getItems().clear();
assignClassesToTeachersClassListView.getItems().clear();
//Populate dropdown lists
addStudentSelectClassesMenuButton.getItems().setAll(checkMenuItemList);
addTeacherSelectClasses.getItems().setAll(checkMenuItemList);
}
}
This function is called from another function after the user inputs a json file that is parsed for data.
The problem im running into is when i try to use .getItems().addAll() it only works once, in my code if you comment one of the two lines the other one will work and vice versa, its strange since they work on their own but not together
Since both of them work on their own I'm not sure what the issue would be thats causing it not too update. There is no error or exception simply nothing happens. After both of the lines executes and before the function completes while debugging it says the both menubuttons have 6 items but when you click on the menu button nothing happens
The issue is NOT:
it definitely can be used multiple times.
Will output as expected:
However, you need to use APIs correctly in context.
Items in the scene graph can only be in one position at a time. A
CheckMenuItemis not a node, but it is probably backed by nodes and thus acts like a node, so I wouldn't add a single instance to more than one menu at a time.Instead, create another
CheckMenuIteminstance, with the same data, and add that. Bidirectional binding can be used to ensure that if one menu item is checked, the other menu item's state is updated to reflect that, and vice versa.See the scene javadoc:
Also, the node javadoc:
It would appear that
CheckMenuItemacts the same way. It would probably be better if theMenudocumentation stated that items can only appear in one menu at a time.Examples to demonstrate failure and fixes
In this example, two menus are created and the same items are added to both menus. On execution, only one of the menus (the last one to which the items were added) will contain the added items.
The execution warns you, in the system error console, that there is a problem.
Broken code
We can fix this by just creating new check menu items for each menu.
But now the check menu items aren't in sync, if you change one, the other one doesn't automatically change. If you also want that behavior, you can use an MVC approach with a shared binding.
Fixed code with bidirectional binding to model properties