title: Get XY position of caret position in JTextArea
body: <p>Actually, i have already ask this question in <a href="https://stackoverflow.com/questions/18864368/get-xy-position-from-caret-position-in-jtextarea/18864392?noredirect=1#18864392">here</a>. But, i'm making mistake. I haven't already get the solution.</p>
  
  <p>First, at the question before, i can get Rectangle with</p>
  
  <pre><code>Rectangle rectangle = textArea.modelToView( textArea.getCaretPostion() );
  </code></pre>
  
  <p>I'm also get X and Y position.</p>
  
  <p>I'm creating a editor that can add new Text Area each i press Enter key. XY position with code above always give same return in every Text Area. Look my code.</p>
  
  <pre><code>import java.awt.Container;
  import java.awt.Font;
  import java.awt.Rectangle;
  import java.awt.event.ActionEvent;
  import java.awt.event.KeyEvent;
  import java.util.LinkedList;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.swing.AbstractAction;
  import javax.swing.Action;
  import javax.swing.Box;
  import javax.swing.JFrame;
  import javax.swing.JTextArea;
  import javax.swing.KeyStroke;
  import javax.swing.text.BadLocationException;
  import javax.swing.text.JTextComponent;
  
  public class forquestion extends JFrame {
  
      Container textAreaBox;
      LinkedList&lt;JTextComponent&gt; textArea;
      int nameTA;
  
      public forquestion() {
              int nameTA = 0;
  
              textArea = new LinkedList&lt;&gt;();
  
              textAreaBox = Box.createVerticalBox();
              textAreaBox.add(Box.createVerticalGlue());
              addLine();
              this.add(textAreaBox);
              this.setVisible(true);
      }
  
      public static void main(String[] args) {
              forquestion app = new forquestion();
              app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      }
  
      public void addLine () {
  
              JTextComponent temp_ta = createTextComponent();
              textArea.add(temp_ta);
              textAreaBox.add(textArea.getLast());
              textAreaBox.add(Box.createVerticalGlue());
      }
  
      protected JTextComponent createTextComponent() {
              JTextArea ta = new JTextArea("test");
  
              /*if (count%2==0)
                              ta.setForeground(Color.red);
              else
                              ta.setForeground(Color.GREEN);*/
  
              ta.setFont(new Font("Courier New",Font.PLAIN,16));
              ta.setLineWrap(true);                                                                                                                                                                                                                                                   
              ta.setWrapStyleWord(true);
              ta.setName(Integer.toString(nameTA));
              nameTA+=1;
  
              basicKey("ENTER", enter, ta);
  
              ta.addMouseListener(new java.awt.event.MouseAdapter() {
                      public void mousePressed(java.awt.event.MouseEvent ev) {
                              try {
                                      taMousePressed(ev);
                              } catch (BadLocationException ex) {
                                      Logger.getLogger(forquestion.class.getName()).log(Level.SEVERE, null, ex);
                              }
                      }
              });
  
              return ta;
      }
  
      public void basicKey(String s, Action a, JTextArea ta) {
  
              ta.getInputMap().put(KeyStroke.getKeyStroke(s), s);
              ta.getActionMap().put(s, a);
      }
  
      Action enter = new AbstractAction() {
  
              @Override
              public void actionPerformed(ActionEvent e) {
  
                      addLine();
              }
      };
  
      private void taMousePressed(java.awt.event.MouseEvent ev) throws BadLocationException {
              int now_focus = Integer.parseInt(ev.getComponent().getName());
              int _caret;
              _caret = textArea.get(now_focus).getCaretPosition();
              Rectangle rectangle = textArea.get(now_focus).modelToView(_caret);
              double x = rectangle.getX();
              //int xc = textArea.get(now_focus).getLocation().x;
              double y = rectangle.getY();
              //int yc = textArea.get(now_focus).getLocation().y;
              //double h = rectangle.getHeight();
              //double w = rectangle.getWidth();
              System.out.println(x);
              System.out.println(y);  
              //System.out.println(xc);
              //System.out.println(yc);  
              //System.out.println(h);
              //System.out.println(w);
              System.out.println("");
      }
  
  }
  </code></pre>
  
  <p>My code will print XY position each time you press a Text Area. But, the display always same in every text area. (Try to make many Text Area and give some text) Btw, it just simple code. You need change the window frame size for update the new text area after you press enter key..hahaha.</p>
  
  <p>So, my question is: How can i get the XY position of caret (text cursor) in any Text Area. I want to display JPopmenu there. :)</p>
  
  <p>I hope this question clear for you. Thx before.</p>