Вопрос

How to get Caret position in pixels in JTextField? There is a method getCaret().getDot(), but it returns symbol count, not pixels. Is there some method besides FontMetrics?

Это было полезно?

Решение

From the API you can use:

int pixelPosition = textField.modelToView( textField.getCaretPosition() ).x;

Другие советы

Given JTextField someJTextField the following code should give you the pixel position of the care:

FontMetrics fm = myJTextField.getFontMetrics();
int caret = someJTextField.getCaret().getDot();


int pixelPosition = fm.stringWidth(myJTextField.getText(0, caret)) -
                                   myJTextField.getScrollOffset();
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top