Java Swing: glyphview: stateinvarianterror (htmldocument) after deleting ordered / unordered list

I don't think this is a new problem However, as long as you use the backspace key to delete the ordered / unordered list (editorkit – > htmleditorkit, document – > htmldocument) in jtextpane all the way to the top, an error will appear The following is the exception thrown by the gettext () method of glyphview

Exception in thread "AWT-EventQueue-0" javax.swing.text.StateInvariantError: GlyphView: Stale view: javax.swing.text.BadLocationException: Invalid location

I can provide sscce for this But the simulation is not very difficult Just use jtextpane with htmleditorkit and htmldocument model in it Use a custom "insertordedlist" operation or insert a string in some way

<HTML><HEAD></HEAD><BODY><UL><LI></LI></UL></BODY></HTML>

This will cause an ordered / unordered list to be inserted in the text pane

The strange part of this bug is as follows:

>Once you start deleting characters (if you happen to have a line below the bullet list), the characters will be deleted until you click the last character of the last bullet Once you reach this, the caret will refuse to move up and the glyphview error will be thrown. > Sometimes, after deleting most characters - you still can't delete the first bullet in the list It just hangs until you execute Ctrl A and then backspace

I have seen these errors in almost all Java - based HTML editors. Except jweb engine, this behavior does not exist Unfortunately, jwebengine is not open source, so I can't see how they solve this problem in their code

My guess is that the notification from the HTML document model has some problems because the cursor positioning code doesn't work properly I also searched the sun error database to see if this particular problem had been raised, but I couldn't find any problems (although I've seen many very similar errors) In addition, I'm pretty sure someone must notice this before and must bring it to the attention of the swing team

Does anyone use the swing (especially the text) section to know if the problem has been raised with sun, or if there are any known solutions to alleviate the problem?

Although the user can still select to delete the list from the pane with the mouse, it just looks strange that he does not choose to use the backspace key to do the same

Sscce is now attached To reproduce this error Follow the steps shown in the attached drawings

>Add a line of text Then click the button above the text pane to add a 2 / 3 bullet item Now place the caret at the end of the last character of the last bullet and continue to press the entire backspace key until all characters are deleted

Observed behavior: the last bullet will hang (not be deleted) and throw an exception (as described above)

Expected: no exceptions, the contents of the text pane should be cleared

public class Test {
    static final JFrame frame = new JFrame ();
    static final JTextPane textPane = new JTextPane ();

    static EditorKit kit;
    static JButton listButton;

    public static void createAndShowGUI () {
        //Create frame
        frame.setSize(400,600);
        frame.setVisible(true);
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);

        //Customize text pane visual properties
        textPane.setSize(300,500);
        textPane.setLocation(50,50);

        //customize text pane non visual properties
        kit = new CustomEditorKit ();
        textPane.setEditorKitForContentType("text/html",kit);
        textPane.setContentType("text/html");

        Action[] actions = ((HTMLEditorKit) kit).getActions();
        Action action = null;
        for (int i = 0; i < actions.length; i++) {
            action = actions [i];
            if (action.getValue(Action.NAME).equals("InsertUnorderedList")) {
                break;
            }
        }
        listButton = new JButton (action);
        listButton.setText("List");
        listButton.setSize(100,20);
        listButton.setLocation(100,10);
        listButton.setVisible(true);

        /* Add button and text pane to frame */
        frame.add(listButton);
        frame.add(textPane);
    }

    public static void main(String[] args) {
        try {
            EventQueue.invokeAndWait(new Runnable () {
                @Override
                public void run() {
                    createAndShowGUI ();
                }
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    static class CustomEditorKit extends HTMLEditorKit {
        @Override
        public Document createDefaultDocument () {
            return new HTMLDocument (this.getStyleSheet());
        }
    }
}

Solution

I used this

action=new HTMLEditorKit.InsertHTMLTextAction("test","<UL><LI><P>\n</P></LI></UL>",HTML.Tag.BODY,HTML.Tag.UL);

Instead of the default operation in your example to provide the correct structure

It's good for me (Win 7,Java 1.6)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>