Java Swing How to - Create custom strike style color in JTextPane








Question

We would like to know how to create custom strike style color in JTextPane.

Answer

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Shape;
/*w w w.j  a va  2s  .  com*/
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BoxView;
import javax.swing.text.ComponentView;
import javax.swing.text.Element;
import javax.swing.text.IconView;
import javax.swing.text.LabelView;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.ParagraphView;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;

public class Main {
  public static void main(String[] args) {
    JFrame fr = new JFrame();
    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JEditorPane pane = new JEditorPane();
    pane.setEditorKit(new NewEditorKit());
    pane.setText("test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test ");

    StyledDocument doc = (StyledDocument) pane.getDocument();
    MutableAttributeSet attr = new SimpleAttributeSet();
    attr.addAttribute("strike-color", Color.red);
    doc.setCharacterAttributes(0, 9, attr, false);

    attr.addAttribute("strike-color", Color.blue);
    doc.setCharacterAttributes(10, 19, attr, false);
    JScrollPane sp = new JScrollPane(pane);

    fr.getContentPane().add(sp);
    fr.setSize(300, 300);
    fr.setLocationRelativeTo(null);
    fr.setVisible(true);
  }
}

class NewEditorKit extends StyledEditorKit {
  public ViewFactory getViewFactory() {
    return new NewViewFactory();
  }
}

class NewViewFactory implements ViewFactory {
  public View create(Element elem) {
    String kind = elem.getName();
    if (kind != null) {
      if (kind.equals(AbstractDocument.ContentElementName)) {
        return new MyLabelView(elem);
      } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
        return new ParagraphView(elem);
      } else if (kind.equals(AbstractDocument.SectionElementName)) {
        return new BoxView(elem, View.Y_AXIS);
      } else if (kind.equals(StyleConstants.ComponentElementName)) {
        return new ComponentView(elem);
      } else if (kind.equals(StyleConstants.IconElementName)) {
        return new IconView(elem);
      }
    }
    return new LabelView(elem);
  }
}

class MyLabelView extends LabelView {

  public MyLabelView(Element elem) {
    super(elem);
  }

  public void paint(Graphics g, Shape allocation) {
    super.paint(g, allocation);
    paintStrikeLine(g, allocation);
  }

  public void paintStrikeLine(Graphics g, Shape a) {
    Color c = (Color) getElement().getAttributes().getAttribute("strike-color");
    if (c != null) {
      int y = a.getBounds().y + a.getBounds().height
          - (int) getGlyphPainter().getDescent(this);
      y = y - (int) (getGlyphPainter().getAscent(this) * 0.3f);
      int x1 = (int) a.getBounds().getX();
      int x2 = (int) (a.getBounds().getX() + a.getBounds().getWidth());

      Color old = g.getColor();
      g.setColor(c);
      g.drawLine(x1, y, x2, y);
      g.setColor(old);
    }
  }
}