I'm trying to set up a character manager for a LARP game. In the game a character could have more than one role (1 or 2). I want to generate the character using two comboboxes, both drawing from the same enum
called Role
. That in itself is easy:
JComboBox roleFirstComboBox = new JComboBox(IPlayerCharacter.Role.values());
JComboBox roleSeondComboBox = new JComboBox(IPlayerCharacter.Role.values());
Except if say our roles are: Coder, Programmer, SysAdmin, Nerdfighter
you can be a Coder/Coder
. So the second box needs to exclude whatever is selected in the first box.
One thought I had was making a function to pass the enums to a List of some sort and then when one JComboBox is picked, it uses one of the standard container methods to find the asynchronous union(?) everything in Box2 that isn't in Box1. This seems horrible. I know the solution uses a JComboBoxModel but how to adapt this to my enums, I don't know.
What is the best way to get this kind of functionality?
Edit:
Here is the code I'm using currently, it just lives inside my pane, so I don't think it needs anymore context. Let me know otherwise, if need be.
Creating the comboBoxes
JComboBox roleFirstComboBox = null;
JComboBox roleSecondComboBox = null;
...
roleFirstComboBox = new JComboBox(IPlayerCharacter.Role.values());
roleSecondComboBox = new JComboBox(IPlayerCharacter.Role.values());
Adding an actionListener:
roleFirstComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
roleSecondComboBox.removeAll();
roleSecondComboBox.addItem(null);
for (Role role : IPlayerCharacter.Role.values()) {
if (role != roleFirstComboBox.getSelectedItem()) {
roleSecondComboBox.addItem(role);
}
}
}
});
roleFirstComboBox.setSelectedIndex(0);
Adding it to the groupLayout:
.addComponent(roleFirstComboBox)
.addComponent(roleSecondComboBox))
The final look, and bug:
Does this help?