# Limit amount of specific component per Container

**URL:** <https://forum.inductiveautomation.com/t/limit-amount-of-specific-component-per-container/92510>\
**Category:** Module Development\
**Tags:** vision\
**Created:** [June 28, 2024, 12:41am UTC](https://forum.inductiveautomation.com/t/limit-amount-of-specific-component-per-container/92510 "2024-06-28T00:41:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathias\_Melling](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mathias_melling/32/67682_2.png) [@Mathias\_Melling](https://forum.inductiveautomation.com/u/Mathias_Melling)\
**Post date:** [June 28, 2024, 12:41am UTC](https://forum.inductiveautomation.com/t/limit-amount-of-specific-component-per-container/92510/1 "2024-06-28T00:41:12Z")

</div>

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:

> [@Component outline on different z-order than component](https://forum.inductiveautomation.com/t/component-outline-on-different-z-order-than-component/92128):
>
> I'm drawing an outline around my component when the mouse hovers over it. Sometimes the component needs to be partially covered by another component, but I still want to show the full outline when the mouse hovers over it. The video below should make it clear what I'm trying to achieve. Does anyone have any advice?

I want to only allow one of these to be placed on each window.

So far I tried the following in the `OutlineHandlerComponent` itself:

```python
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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 28, 2024, 12:54am UTC](https://forum.inductiveautomation.com/t/limit-amount-of-specific-component-per-container/92510/2 "2024-06-28T00:54:13Z")

</div>

I doubt you can prevent multiples. You **can** cause the duplicates to render an obvious and annoying error image.

---

<div class="post-metadata">

**Author:** ![Mathias\_Melling](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mathias_melling/32/67682_2.png) [@Mathias\_Melling](https://forum.inductiveautomation.com/u/Mathias_Melling)\
**Post date:** [June 28, 2024, 1:53am UTC](https://forum.inductiveautomation.com/t/limit-amount-of-specific-component-per-container/92510/3 "2024-06-28T01:53:49Z")

</div>

Well, the `NullPointerException` is thrown because the just deleted component is still selected in the Designer. As soon as I select something else, there are no more exceptions. So I've been wanting to select the root container programmatically before removing the duplicate `OutlineHandlerComponent`, but I can't figure out how to actually select something via code.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 28, 2024, 2:13am UTC](https://forum.inductiveautomation.com/t/limit-amount-of-specific-component-per-container/92510/4 "2024-06-28T02:13:03Z")

</div>

Perhaps you can remove the _other_ one. (:

---

<div class="post-metadata">

**Author:** ![Mathias\_Melling](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mathias_melling/32/67682_2.png) [@Mathias\_Melling](https://forum.inductiveautomation.com/u/Mathias_Melling)\
**Post date:** [June 28, 2024, 2:23am UTC](https://forum.inductiveautomation.com/t/limit-amount-of-specific-component-per-container/92510/5 "2024-06-28T02:23:52Z")

</div>

Yeah, that does work. But the problem there is that all the components that need an outline are already subscribed to the first `OutlineHandlerComponent`, and if it is deleted, then they will complain.
