title: Listen for a mouse click event inside a for loop and use the coordinates from that event in later code
body: <p>What I want to do is inside the rest of my code have it wait for and recognise a mouse click event and then be able to use the coordinates of that click in the rest of the code, not just print it to the screen on click as pretty much every other example I have found does.</p>
  <p>I am new to java and this is the first time I'm making an interface so I do apologise for any mistakes and I will fix them as soon as I can.</p>
  <p>Unfortunately, as I don't have a clue how to do what I am asking for, I can only represent it in a sort of pseudocode crossed with bits of code or I wouldn't be asking here in the first place.</p>
  <p>edit: Below comment says don't do this, which I understand but I really need to be able to get those coordinates to be usable in that area of code (which is a lot longer than shown below) so if anyone has any alternative ways in which I could achieve that then please share.</p>
  <p>It is the two clicks at two different locations to get two different coordinates to then manipulate that is really throwing me.</p>
  <pre><code>.....
  for(int i = 0; i &lt; 15; i++) {
      wait for mouse click;
      int x = x coordinate of mouse click;
      int y = y coordinate of mouse click;
      wait for mouse click;
      int x2 = x coordinate of second mouse click;
      int y2 = y coordinate of second mouse click;
      if (closestVertex(x,y) != closestVertex(x2,y2) &amp;&amp; ! graph.isline(x,y,x2,y2)) {
          graph.insertLine(x,y,x2,y2);
      }
  }
  .... etc...
  </code></pre>
  <p>So far the solutions I have seen have involved looking at where the pointer is at a given moment but that is not the location only at the moment when clicked. This type of solution is illustrated below:</p>
  <pre><code>int x = MouseInfo.getPointerInfo().getLocation.x;
  int y = MouseInfo.getPointerInfo().getLocation.y;
  </code></pre>
  <p>And also another common one that crops up is altering the MouseListener as below:</p>
  <pre><code>@Override
  public void mouseClicked(MouseEvent e) {
      int x=e.getX();
      int y=e.getY();
      System.out.println(x+&quot;,&quot;+y);
  }
  </code></pre>
  <p>But for this option, yes, you can print the coordinate, but I don't see a way that I can use the coordinate details outside of the mouseClicked function as I would like.</p>
  <p>In terms of getting it to wait for the action to occur and not just keep going through the loop, I have a couple of ideas. I thought of adding a while loop inside and using wait() and Thread.sleep(100) but the issue is I had no idea of how to say essentially:</p>
  <pre><code>while(event hasn't occurred) {
      wait()
  }
  int x = x coordinate of mouse click;
  int y = y coordinate of mouse click;
  ...etc
  </code></pre>
  <p>If you have any new ideas I could try I would greatly appreciate it as I have been through I don't even know how many links on google looking for a way to make this work.</p>