简体   繁体   English

具有HTML的JTextPane的文本光标

[英]Text cursor for JTextPane with HTML

I have the following problem: I want to change the cursor of a JTextPane with content type text/html to Cursor.TEXT_CURSOR . 我有以下问题:我想将内容类型为text/htmlJTextPane的光标更改为Cursor.TEXT_CURSOR However, when setting setCursor(new Cursor(Cursor.TEXT_CURSOR)) it is ignored. 但是,当设置setCursor(new Cursor(Cursor.TEXT_CURSOR))它将被忽略。 I also tried to set the cursor in the mouse listener, but it is also directly changes back to the standard cursor. 我也尝试在鼠标侦听器中设置光标,但是它也直接更改回标准光标。 If the content type is text/plain , the cursor is by default the text cursor. 如果内容类型为text/plain ,则默认情况下,光标为文本光标。 Does any one has an idea how to reach this goal? 有谁知道如何实现这一目标? I created an SCCEE to show this behavior: 我创建了一个SCCEE来显示此行为:

import java.awt.Cursor;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;


public class TextPaneHtmlCursor extends JFrame {
    private JScrollPane jScrollPane1;
    private JTextPane jTextPane1;     

    public TextPaneHtmlCursor() {
        initComponents();
    }

    private void initComponents() {
        jScrollPane1 = new JScrollPane();
        jTextPane1 = new JTextPane();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        jTextPane1.setContentType("text/html");
        jTextPane1.setCursor(new Cursor(Cursor.TEXT_CURSOR));
        jScrollPane1.setViewportView(jTextPane1);
        getContentPane().add(jScrollPane1);
        pack();
    }                    

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TextPaneHtmlCursor().setVisible(true);
            }
        });
    }        
}

Thank you very much! 非常感谢你!

So after a little bit more digging, it would seem the the EditorKit (in this case HTMLEditorKit ) is responsible for making the decisions about what cursor should be used. 因此,经过多一点的挖掘,似乎EditorKit (在本例中为HTMLEditorKit )负责做出有关应使用哪种光标的决定。

You can change the "default" cursor using something like... 您可以使用类似...的方法来更改“默认”光标。

jTextPane1.setContentType("text/html");
((HTMLEditorKit)tp.getEditorKit()).setDefaultCursor(cursor);

The default "default" is defined as private static final Cursor DefaultCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); 默认的“默认”定义为private static final Cursor DefaultCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); which is very annoying... 这很烦人...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM