2010-12-30 75 views
6

JTextAreaのカーソル位置をピクセルで見つけるのに役立つだろうか? 私はtxtArea.getCaretPosition()を使用しました。しかし、これは私が期待しているポジションではありません。実際には、ピクセルの座標x,yのカーソル位置が必要です。JTextAreaのカーソル位置を見つける方法

答えて

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(); 
      } 
     }); 
    } 
} 
+1

+1でJPopupを表示する例です。このためにUIを使用する必要はありません。 textComponent.modelToView(...)を使用できます。 – camickr

+0

@camickr:良い点、私はJTextComponentのAPIをスクロールするときにこれを見逃してしまったのですが、TextUIでそれが「深い」ことを発見したことに驚いています:)私は思っています。あなたのコメントに、または "歴史"を保存するためにそれを保つ? – Uhlen

+0

良い質問です。 2つの共通プラクティスには、1)別のアプローチで別の回答を投稿し、2)関連する「編集」履歴セクションにリンクを追加する。 – trashgod

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); 
関連する問題