Как остановить java HTMLEditorKit от автоматического закрытия моих тегов - PullRequest
1 голос
/ 27 марта 2020

У меня есть проблема, когда мой жирный тег автоматически закрывается java HTMLEditorKit, я несколько раз пытался решить эту проблему, но я застрял здесь. Я использую панель JEditorPane. Когда я нажимаю полужирную кнопку, тег должен оставаться открытым, пока я не нажму кнопку еще раз. Вот мой код

    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) {
                }
            }
        }
    });
...