• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*JTextAreaのキャレットを上書きモード対応にする [#bce20037]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-01-16~
更新日:&lastmod;
---
category: swing
folder: OverTypeMode
title: JTextAreaのキャレットを上書きモード対応にする
tags: [JTextArea, Caret]
author: aterai
pubdate: 2006-01-16T15:56:38+09:00
description: JTextAreaにキャレット上の文字を上書きする上書きモードを追加します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTQtaGO6gI/AAAAAAAAAgA/XPqHe-c_DUo/s800/OverTypeMode.png
---
* 概要 [#summary]
`JTextArea`にキャレット上の文字を上書きする上書きモードを追加します。[https://community.oracle.com/tech/developers/discussion/1385467 JTextPane edit mode (insert or overwrite)???]のソースコードを変更して全角文字対応にしています。

#contents
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTQtaGO6gI/AAAAAAAAAgA/XPqHe-c_DUo/s800/OverTypeMode.png)

**概要 [#l6341a56]
JTextAreaにキャレット上の文字を上書きする上書きモードを追加します。
* サンプルコード [#sourcecode]
#code(link){{
// Paint a horizontal line the width of a column and 1 pixel high
class OvertypeCaret extends DefaultCaret {
  // The overtype caret will simply be a horizontal line
  // one pixel high (once we determine where to paint it)
  @Override public void paint(Graphics g) {
    if (isVisible()) {
      try {
        JTextComponent component = getComponent();
        TextUI mapper = component.getUI();
        Rectangle r = mapper.modelToView(component, getDot());
        g.setColor(component.getCaretColor());
        int width = g.getFontMetrics().charWidth('w');
        // 全角などに対応
        if (isOvertypeMode()) {
          int pos = getCaretPosition();
          if (pos < getDocument().getLength()) {
            if (getSelectedText() != null) {
              width = 0;
            } else {
              String str = getText(pos, 1);
              width = g.getFontMetrics().stringWidth(str);
            }
          }
        } // ここまで追加
        int y = r.y + r.height - 2;
        g.drawLine(r.x, y, r.x + width - 2, y);
      } catch (BadLocationException e) {}
    }
  }

[[Java Forums - JTextPane edit mode (insert or overwrite)???>http://forum.java.sun.com/thread.jspa?forumID=57&threadID=667407]]のソースコードを変更して全角文字対応にしています。
  // Damage must be overridden whenever the paint method is overridden
  // (The damaged area is the area the caret is painted in. We must
  // consider the area for the default caret and this caret)
  @Override protected synchronized void damage(Rectangle r) {
    if (r != null) {
      JTextComponent c = getComponent();
      x = r.x;
      y = r.y;
      // width = c.getFontMetrics(c.getFont()).charWidth('w');
      // 全角対応
      width = c.getFontMetrics(c.getFont()).charWidth('あ');
      height = r.height;
      repaint();
    }
  }
}
}}

#screenshot
* 解説 [#explanation]
上記のサンプルでは、`DefaultCaret#paint(...)`メソッドなどをオーバーライドした上書きモード用の`Caret`を作成しKBD{Insert}キーでこの`Caret`を表示するモードに切り替えることができます。

**サンプルコード [#hf545cfe]
 // Paint a horizontal line the width of a column and 1 pixel high
 class OvertypeCaret extends DefaultCaret {
   //The overtype caret will simply be a horizontal line
   //one pixel high (once we determine where to paint it)
   public void paint(Graphics g) {
     if(isVisible()) {
       try{
         JTextComponent component = getComponent();
         TextUI mapper = component.getUI();
         Rectangle r = mapper.modelToView(component, getDot());
         g.setColor(component.getCaretColor());
         int width = g.getFontMetrics().charWidth('w');
         //全角などに対応
         if(isOvertypeMode()) {
           int pos = getCaretPosition();
           if(pos<getDocument().getLength()) {
             if(getSelectedText()!=null) {
               width = 0;
             }else{
               String str = getText(pos, 1);
               width = g.getFontMetrics().stringWidth(str);
             }
           }
         } //ここまで追加
         int y = r.y + r.height - 2;
         g.drawLine(r.x, y, r.x + width - 2, y);
       }catch(BadLocationException e) {}
     }
   }
   // Damage must be overridden whenever the paint method is overridden
   // (The damaged area is the area the caret is painted in. We must
   // consider the area for the default caret and this caret)
   protected synchronized void damage(Rectangle r) {
     if(r != null) {
       JTextComponent c = getComponent();
       x = r.x;
       y = r.y;
       //width = c.getFontMetrics(c.getFont()).charWidth('w');
       //全角対応
       width = c.getFontMetrics(c.getFont()).charWidth('あ');
       height = r.height;
       repaint();
     }
   }
 }
- 上書きモード自体の動作は`JTextArea#replaceSelection(...)`メソッドをオーバーライドすることで実現
-- ここでキー入力を検知したとき次の文字までを選択置換する処理を実行

-&jnlp;
-&jar;
-&zip;
* 参考リンク [#reference]
- [https://community.oracle.com/tech/developers/discussion/1385467 JTextPane edit mode (insert or overwrite)???]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/text/DefaultCaret.html#paint-java.awt.Graphics- DefaultCaret#paint(...) (Java Platform SE 8)]

**解説 [#o73b8338]
上記のサンプルでは、上書きモード用のキャレットを作成して、Insertキーで切り替えるようになっています。
* コメント [#comment]
#comment
- 改行の処理がまだいい加減です。 -- &user(aterai); &new{2006-01-16 (月) 15:56:38};
- テキストを選択状態にしてKBD{Insert}キーを押すと、反転されたままになるようなので修正。 -- &user(aterai); &new{2006-01-16 (月) 18:45:02};
- `IME`起動時、「あ」を入力し、KBD{変換}キー(or KBD{Space}キー)を押すたびに減っていきます。日本語対応のソースを心待ちにしています。 -- &user(初心者); &new{2006-03-24 (金) 10:40:01};
- バグですね。報告ありがとうございます。減らないように修正はしたのですが、置換は以下のように適当です。 -- &user(aterai); &new{2006-03-24 (金) 16:26:25};

上書きモード自体の動作は、JTextArea#replaceSelectionメソッドをオーバーライドすることで行われています。上書きモードの場合、次の文字までを選択して置き換える処理がここで追加されています。

**参考リンク [#c7f33248]
-[[Java Forums - JTextPane edit mode (insert or overwrite)???>http://forum.java.sun.com/thread.jspa?forumID=57&threadID=667407]]

**コメント [#n3e0ddb6]
- 改行の処理がまだいい加減です。 -- [[terai]] &new{2006-01-16 (月) 15:56:38};
- テキストを選択状態にしてInsertキーを押すと、反転されたままになるようなので修正。 -- [[terai]] &new{2006-01-16 (月) 18:45:02};
- IME起動時、「あ」を入力し、変換キー(or spaceキー)を押すたびに減っていきます。日本語対応のソースを心待ちにしています。 -- [[初心者]] &new{2006-03-24 (金) 10:40:01};
- バグですね。報告ありがとうございます。減らないように修正はしたのですが、置換は以下のように適当です。 -- [[terai]] &new{2006-03-24 (金) 16:26:25};
 aaaabbbb   //元のテキストの先頭で「ああ」と入力、変換して確定すると
 嗚呼bbbb   //こうならずに
 嗚呼aabbbb //こうなってしまう(まじめにやれば、なんとかなりそうだが)
- 早速のご対応ありがとうございます。m時 -- [[初心者]] &new{2006-03-27 (月) 14:09:27};
- ごめんなさい。途中で送信してしまいました。文字という点で、半角1文字の上書きが全角1文字でOKだと個人的には思います。 -- [[初心者]] &new{2006-03-27 (月) 14:10:36};
- xyzzyを参考にして「嗚呼bbbb」の方がいかなと思ったのですが、さっき試したexcelだと「嗚呼aabbbb」になるみたいです。ので、これは仕様ということにしておきます(^^;。 -- [[terai]] &new{2006-03-27 (月) 15:00:06};
- xyzzyは使ったことがないのですが、秀丸と同じでしょうか。「嗚呼」に「a」と入力して、「a 呼」なのは、ちょっといやでした。excel上書できるんですね。wordでIME起動中にInsertキー押下したら、ダイアログに驚きました。 -- [[初心者]] &new{2006-03-28 (火) 13:05:15};

- 早速のご対応ありがとうございます。m時 -- &user(初心者); &new{2006-03-27 (月) 14:09:27};
- ごめんなさい。途中で送信してしまいました。文字という点で、半角`1`文字の上書きが全角`1`文字でOKだと個人的には思います。 -- &user(初心者); &new{2006-03-27 (月) 14:10:36};
- `xyzzy`を参考にして「嗚呼bbbb」の方がいかなと思ったのですが、さっき試した`excel`だと「嗚呼aabbbb」になるみたいです。ので、これは仕様ということにしておきます(^^;。 -- &user(aterai); &new{2006-03-27 (月) 15:00:06};
- `xyzzy`は使ったことがないのですが、秀丸と同じでしょうか。「嗚呼」に「a」と入力して、「a 呼」なのは、ちょっといやでした。`excel`上書できるんですね。`word`で`IME`起動中にKBD{Insert}キー押下したら、ダイアログに驚きました。 -- &user(初心者); &new{2006-03-28 (火) 13:05:15};
- `xyzzy`でも「a 呼」です。他の文字の位置が上書きで可能な限り移動しないようにするための処理だと思います。 -- &user(aterai); &new{2006-03-31 (金) 16:39:04};

#comment