title: Finding position of children Components relative to its parent panel
body: <p>I'm creating background images with a java program which I upload to a site to use to display data. The images are based on parameters which create unique views and datapoints which i can upload to the server.</p>
  
  <p>On the site, I add items to display data at specific locations on the screen. I've made it so my java program can create the backgrounds and the data points, but i have to manually configure the location of the data points by finding the right positions and configuring them as I go.</p>
  
  <p>I would like to figure out the same positions of specific labels and panels inside my parent panel of my java program so i can store the points and use them later to generate positions for my data points so it can be automatized in the future.</p>
  
  <p>My background structure is like so</p>
  
  <p>Frame - Holds a JTabbedPane which switches between 4-10 different panels.</p>
  
  <p>Each one of the tabbedpane panels is the main panel which becomes a background. </p>
  
  <p>Inside each panel there are multiple subpanels which are organized using a GridbagLayout.</p>
  
  <p>Each subpanel can have its own panels or labels which are positions im looking to record.</p>
  
  <p>Here is a picture showing an example of one of the panels. The red boxes are the locations of the labels im looking to get a position of.</p>
  
  <p><a href="https://i.stack.imgur.com/enc4S.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/enc4S.png" alt="Pic"></a></p>
  
  <p>I tried using this code to find the positions and put them on the label but the positions are always at 0,0</p>
  
  <pre><code>public void positions(Container p1) {
  
      for (Component p : p1.getComponents()) {
          if (p instanceof JLabel) {
  
              try {
                  if (((JLabel) p).getText().isEmpty()) {
                      Point spot = ((JLabel) p).getLocation();
                      ((JLabel) p).setText(spot.toString());
                  }
              } catch (NullPointerException e) {
  
              }
              //System.out.println(e.getMessage());
          } else {
              if (p instanceof JPanel) {
                  positions((Container) p);
              }
          }
      }
  
  }
  </code></pre>
  
  <p>This calls the function at the end of my update</p>
  
  <pre><code>mainPanel.revalidate();
  mainPanel.repaint();
  positions(mainPanel);
  </code></pre>
  
  <p>Update. I made a call to the above function from my frame class which updates each panel on the tabbed pane, then packs it, and then calls the function.</p>
  
  <p><a href="https://i.stack.imgur.com/Qk3cs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qk3cs.png" alt="Output"></a></p>
  
  <p>As you can see the positions are relative it its panel containing the label, rather than the panel which contains all the subpanels.</p>