JTextPane - Make Caret Normal Size?

眉间皱痕 提交于 2019-12-11 05:23:06

问题


So I am trying to make the text in a JTextPane double spaced. Here's my code:

     MutableAttributeSet attrs = editor.getInputAttributes();
     StyleConstants.setLineSpacing(attrs, 2);
     editor.getStyledDocument().setParagraphAttributes(0, doc.getLength() + 1, attrs, true);

The problem with this, is, the curser (caret) is as big as three or four line spaces. How can I resize the caret to normal size?

Here is a screen snip


回答1:


Try this:

editor.setCaret(new DefaultCaret() {

    public void paint(Graphics g) {

        JTextComponent comp = getComponent();
        if (comp == null)
            return;

        Rectangle r = null;
        try {
            r = comp.modelToView(getDot());
            if (r == null)
                return;
        } catch (BadLocationException e) {
            return;
        }
        r.height = 15; //this value changes the caret size
        if (isVisible())
            g.fillRect(r.x, r.y, 1, r.height);
    }
});


来源:https://stackoverflow.com/questions/11809243/jtextpane-make-caret-normal-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!