How would I go about restricting any container within Vision to only allow one component of a specific class? I created an OutlineHandlerComponent
, as seen in this topic:
I want to only allow one of these to be placed on each window.
So far I tried the following in the OutlineHandlerComponent
itself:
protected void onStartup() {
// Check for other Outline Handlers. If one already exists then remove this one, only one is allowed per Container.
for (Component c : getParent().getComponents())
if (c instanceof OutlineHandlerComponent && c != this) {
JOptionPane.showMessageDialog(null, "An Outline Handler already exists on this Container.");
SwingUtilities.invokeLater(() -> {
Container root = getParent();
root.remove(this);
});
break;
}
}
The idea being that if an OutlineHandlerComponent
already exists when this one is placed, then remove itself again. Though this does succesfully remove the newly placed component, it also causes a NullPointerException
to be thrown.