Marco
Marco

Reputation: 11

How to stop java HTMLEditorKit from automatically closing my tags

I have an issue where my bold tag is automatically closed by java HTMLEditorKit, I tried several times to solve this issue, but I am stuck here. I am using a JEditorPane When I press the bold button it should keep the tag open until I click the button again Here is my code

    HTMLEditorKit hkit = new HTMLEditorKit();
    HTMLDocument doc = new HTMLDocument();
    editorPane.setEditorKit(hkit);
    editorPane.setDocument(doc);
    /*editorPane.setText(htmlString);*/
    contentPane.add(panel_2, BorderLayout.SOUTH);
    bbold.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            int start = editorPane.getSelectionStart();
            int end = editorPane.getSelectionEnd();
            if (start == end) {                         //  x=y
                if (btrigger == false) {
                    try {
                        hkit.insertHTML(doc, start, "<b>\u0000", 0, 0, HTML.Tag.B);
                        btrigger = true;
                    } catch (BadLocationException | IOException e) {
                        e.printStackTrace();
                    }
                }
                else {
                    try {
                        hkit.insertHTML(doc, start, "</b>\u0000", 0, 0, HTML.Tag.B);
                        btrigger = true;
                    } catch (BadLocationException | IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            if (start > end) {                          // x<y
                if (btrigger == false) {
                }
            }
        }
    });

Upvotes: 1

Views: 45

Answers (0)

Related Questions