JTextAreaのカーソル位置をピクセルで見つけるのに役立つだろうか? 私はtxtArea.getCaretPosition()
を使用しました。しかし、これは私が期待しているポジションではありません。実際には、ピクセルの座標x,y
のカーソル位置が必要です。JTextAreaのカーソル位置を見つける方法
6
A
答えて
0
FontMetricsクラスを使用する必要があります。
テキストを分析し、テキストの新しい行/折り返しを見つけます。 はその後
....
Font font = new Font("Verdana", Font.PLAIN, 10);
FontMetrics metrics = new FontMetrics(font);
Rectangle2D bounds = metrics.getStringBounds("string", null);
int widthInPixels = (int) bounds.getWidth();
などを使用して、あなたが本当にしたい場合は、クールにここにあなたのソリューションを投稿してください。 :)
3
TextUIは、キャレットのあなたの位置/サイズをあげる方法modelToView
を持っている:
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();
}
});
}
}
0
だけ
Point point = textArea.getCaret().getMagicCaretPosition();
以下のようなポイントが
0
nullになる可能性がありますのでご注意ください
私はここに他の記事を試した後にこれを作った。テキストエリアにテキストの折り返しが無効になっている限り、完全に動作します。例えば
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
Rectangle caretRect=textPane.getUI().modelToView(textPane,textPane.getCaretPosition());
又は
Rectangle caretRect=textPane.modelToView(textPane.getCaretPosition());
。ポップアップを表示するためにこの矩形を使用してください:
popupMenu.show(textPane、caretRect.x、caretRect.y);
2
これらの回答の中には、キャレットの位置を取得しているテキストコンポーネントを基準にした相対的な位置が返されることに注意してください。
あなたは、画面上のキャレットの位置を取得したい場合は、私はあなたがこの方法で使用することをお勧め:ここ
Caret caret = yourTextComponent.getCaret();
Point p = caret.getMagicCaretPosition();
p.x += yourTextComponent.getLocationOnScreen().x;
p.y += yourTextComponent.getLocationOnScreen().y;
aFrameYourePositioning.setLocation(p);
+0
これは魅力のように働いた。これらの座標を使用してJPopupMenuを表示するには他の方法を使用すると問題がありましたが、これは完璧でした。 – Alfabravo
0
は、キャレット位置
Caret caret = logText.getCaret();
Point p = new Point(caret.getMagicCaretPosition());
p.x += logText.getLocationOnScreen().x;
p.y += logText.getLocationOnScreen().y;
SwingUtilities.convertPointFromScreen(p, actionsPanel);
popup.show(actionsPanel, p.x, p.y);
関連する問題
- 1. カーソル位置を見つける
- 2. 正確なカーソル位置を見つけ出す方法
- 3. プログラムでは、Windowsの点滅カーソルの位置を見つける?+ +?
- 4. pdfのテキストのx、yの位置を見つける方法
- 5. ファタフォーマットシーケンスの各塩基の位置を見つける方法
- 6. 階層内のデータアイテムの位置を見つける方法は?
- 7. Webブラウザコントロールで要素の位置を見つける方法は?
- 8. テキストカーソルのグローバルな位置を見つける方法は?
- 9. iosでオリジナル画像のピクセル位置を見つける方法
- 10. Html Agility Packでhtmlタグノードの位置を見つける方法
- 11. horizontalScrollViewで最も右の位置を見つける方法
- 12. 選択肢の位置を見つける方法?
- 13. C++でファイルポインタの位置を見つける方法は?
- 14. 回転したテキストの位置を見つける方法は?
- 15. JSとJSON - 位置の数を見つける方法
- 16. アンドロイドでRecyclerの表示位置を見つける方法は?
- 17. 実行時にマウスの位置を見つける方法は?
- 18. ウィンドウ内の絶対NSViewフレーム位置を見つける方法
- 19. 数字の位置を見つけて使用する方法
- 20. 現在のカーソル位置の後に特定の文字を見つける
- 21. QGraphicsItemのスクリーン位置を見つける
- 22. touchmoveでオフセット位置を見つける方法は?
- 23. HTMLテキストエリア内のカーソル位置(X/Y、行/列ではない)を見つける方法はありますか?
- 24. Cocos2d - CCMoveBy - スプライト位置を見つける
- 25. 位置と向きを見つける
- 26. JTextAreaでテキストの位置を変更する方法
- 27. BITのプリフィックスサムMを持つ位置を見つける方法は?
- 28. 文字列ベースのカーソル位置付け
- 29. VBでのカーソル位置をピクチャボックスの位置に変更する方法
- 30. 仮想キーボードの最上位ピクセル位置を見つける?
+1でJPopupを表示する例です。このためにUIを使用する必要はありません。 textComponent.modelToView(...)を使用できます。 – camickr
@camickr:良い点、私はJTextComponentのAPIをスクロールするときにこれを見逃してしまったのですが、TextUIでそれが「深い」ことを発見したことに驚いています:)私は思っています。あなたのコメントに、または "歴史"を保存するためにそれを保つ? – Uhlen
良い質問です。 2つの共通プラクティスには、1)別のアプローチで別の回答を投稿し、2)関連する「編集」履歴セクションにリンクを追加する。 – trashgod