How to find cursor position in JTextArea

Would help me find the cursor position in JTextArea in terms of a pixel? I used txtArea.getCaretPosition() . But this is not the position that I expect. In fact, I want the cursor position to be in the x,y coordinate of the pixel.

+6
java swing jtextarea
source share
7 answers

TextUI has a modelToView method that will give you the caret position / size:

 import java.awt.BorderLayout; import java.awt.Rectangle; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.SwingUtilities; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.text.BadLocationException; import javax.swing.text.JTextComponent; public class PixelPositionOfCaretDemo { public PixelPositionOfCaretDemo() { JLabel label = new JLabel("Move the caret..."); JTextArea area = new JTextArea(); area.addCaretListener(new MyCaretListener(label)); JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); frame.setBounds(new Rectangle(400, 400, 400, 400)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(area, BorderLayout.CENTER); frame.add(label, BorderLayout.SOUTH); frame.setVisible(true); } private static class MyCaretListener implements CaretListener { private final JLabel label; MyCaretListener(JLabel label) { this.label = label; } @Override public void caretUpdate(CaretEvent e) { JTextComponent textComp = (JTextComponent) e.getSource(); try { Rectangle rect = textComp.getUI().modelToView(textComp, e.getDot()); label.setText(rect.toString()); } catch (BadLocationException ex) { throw new RuntimeException("Failed to get pixel position of caret", ex); } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new PixelPositionOfCaretDemo(); } }); } } 
+3
source share

Note that some of these answers return a location relative to the text component from which you get the caret position.

If you want to get the caret position on the screen, I suggest you use this method:

 Caret caret = yourTextComponent.getCaret(); Point p = caret.getMagicCaretPosition(); px += yourTextComponent.getLocationOnScreen().x; py += yourTextComponent.getLocationOnScreen().y; aFrameYourePositioning.setLocation(p); 
+2
source share

You may need to use the FontMetrics class.
Analyze the text, find out new lines / text wrapping. then use

  Font font = new Font("Verdana", Font.PLAIN, 10); FontMetrics metrics = new FontMetrics(font); Rectangle2D bounds = metrics.getStringBounds("string", null); int widthInPixels = (int) bounds.getWidth(); 

etc....

And if you really want to, post your solution here to be cool. :)

0
source share

As below

 Point point = textArea.getCaret().getMagicCaretPosition(); 

Please note that the point may be zero.

0
source share

I did this by trying other posts here. It works great because text wrapping is disabled in your text box.

 public Point getCaretPixelPosition(JTextArea a) { try { int cpos = a.getCaretPosition(); Font font = a.getStyledDocument().getFont(a.getStyledDocument().getLogicalStyle(cpos)); FontMetrics metrics = getFontMetrics(font); int lineNum = a.getLineOfOffset(cpos); int y = lineNum * metrics.getHeight(); int lineStart = a.getLineStartOffset(lineNum); int x = metrics.stringWidth(a.getText().substring(lineStart, cpos)); return new Point(x,y); } catch(BadLocationException e) {} return null; } 
0
source share
 Rectangle caretRect=textPane.getUI().modelToView(textPane,textPane.getCaretPosition()); 

or

 Rectangle caretRect=textPane.modelToView(textPane.getCaretPosition()); 

eg; use this rectangle to display a popup:

popupMenu.show (textPane, caretRect.x, caretRect.y);

0
source share

Here is an example of displaying JPopup at carriage position

 Caret caret = logText.getCaret(); Point p = new Point(caret.getMagicCaretPosition()); px += logText.getLocationOnScreen().x; py += logText.getLocationOnScreen().y; SwingUtilities.convertPointFromScreen(p, actionsPanel); popup.show(actionsPanel, px, py); 
0
source share

All Articles