概要

JTextFieldJTextPaneなどのJTextComponentで、Caretの色を変更します。

サンプルコード

UIManager.put("TextArea.caretForeground", Color.ORANGE);
JTextArea area = new JTextArea("TextArea.caretForeground: ORANGE");

JTextField field = new JTextField("JTextField");
field.setCaretColor(Color.RED);
View in GitHub: Java, Kotlin

解説

  • JTextField
    • setCaretColor(...)メソッドで、Caretの色を変更
      • nullを設定すると、JTextFieldの文字色がCaretの色になる
    • IMEで変換中のCaretは、setCaretColor(...)での設定を無視してJTextFieldの文字色がCaretの色になる
  • JTextArea
    • UIManager.put("TextArea.caretForeground", Color.ORANGE)Caretの色を変更
    • IMEで変換中のCaretは、setCaretColor(...)での設定を無視してJTextAreaの文字色がCaretの色になる
  • JTextPane
    • setCaretColor(null)で、Caretの色をnullに設定
    • 上記のサンプルでCaretの色は、一行目の途中(二行目の111の幅まで?)まで赤、それ以外は黒になる
    • IMEで変換中のCaretは、setCaretColor(...)での設定を無視してGraphics#setXORMode(...)で反転した色がCaretの色になる
      • 二行目では、行の背景色の緑の反転色のピンク、文字色の赤の反転色の水色になる?(JTextComponent.ComposedTextCaretを参照)
        //
        // Caret implementation for editing the composed text.
        //
        class ComposedTextCaret extends DefaultCaret implements Serializable {
          Color bg;
        
          //
          // Get the background color of the component
          //
          public void install(JTextComponent c) {
            super.install(c);
        
            Document doc = c.getDocument();
            if (doc instanceof StyledDocument) {
              StyledDocument sDoc = (StyledDocument)doc;
              Element elem = sDoc.getCharacterElement(c.composedTextStart.getOffset());
              AttributeSet attr = elem.getAttributes();
              bg = sDoc.getBackground(attr);
            }
        
            if (bg == null) {
              bg = c.getBackground();
            }
          }
        
          //
          // Draw caret in XOR mode.
          //
          public void paint(Graphics g) {
            if (isVisible()) {
              try {
                Rectangle r = component.modelToView(getDot());
                g.setXORMode(bg);
                g.drawLine(r.x, r.y, r.x, r.y + r.height - 1);
                g.setPaintMode();
              } catch (BadLocationException e) {
                // can't render I guess
                //System.err.println("Can't render cursor");
              }
            }
          }
        
          //
          // If some area other than the composed text is clicked by mouse,
          // issue endComposition() to force commit the composed text.
          //
          protected void positionCaret(MouseEvent me) {
            JTextComponent host = component;
            Point pt = new Point(me.getX(), me.getY());
            int offset = host.viewToModel(pt);
            int composedStartIndex = host.composedTextStart.getOffset();
            if ((offset < composedStartIndex) ||
                (offset > composedTextEnd.getOffset())) {
              try {
                // Issue endComposition
                Position newPos = host.getDocument().createPosition(offset);
                host.getInputContext().endComposition();
        
                // Post a caret positioning runnable to assure that the positioning
                // occurs *after* committing the composed text.
                EventQueue.invokeLater(new DoSetCaretPosition(host, newPos));
              } catch (BadLocationException ble) {
                System.err.println(ble);
              }
            } else {
              // Normal processing
              super.positionCaret(me);
            }
          }
        }
        

参考リンク

コメント