JTextPane: Como "pegar" texto formatado (estilizado)?

2 respostas
I

Pessoal

Estou fazendo um editor de texto em um applet, utilizando um JTextPane. Este formata o texto em negrito, itálico e sublinhado. Eu consigo formatar o texto porém não consigo recuperar estas formatações. Consigo apenas recuperar o texto puro…

Como posso fazer para recuperar o texto com as demarcações dos trechos de cada estilo aplicado? Tentei usar o HTMLDocument mas não obtive sucesso.

Obrigado desde já pela ajuda.

2 Respostas

Metaleiro

[color=darkblue] Bem, achei sua idéia interessante, dei uma pesquisada na net e achei esses dois métodos, não sei se ajuda :
[/color]

/**
     * retrieves the body that is encoded with XHTML.  If there isn't a XHTML body, this method
     * will return null.  Normally the body is the same as the getBody() but with additional
     * formatting tags (using XHTML Basic).
     */
    public String getHTMLBody() {
        //<html> should be one level below <message>
        XMLOutputter os = getXMLOutputter();
        String htmlStr = null;
        //html has to be xhtml standards compliant
        Element html = getDOM().getChild("html", htmlNS);
        if (html != null)
            htmlStr = os.outputString(html);
        return htmlStr;
    }
/**
         * @param font The Font, that is to be used. If null, the default of JTextPane is taken.
         * @return the collected HTML text, that has been added to this object.
         * &lt;html&gt; tags are wrapped around the result, if not already there.
         */
        protected String getHtml(Font font) {
                if (!doHtml) {
                        return "";
                }
                String result;
                if (this.linStagesHtml.size() > 0) {
                        String fontface;
                        if (font != null) {
                                //fontface = " style=\"font-size:" + font.getSize()+ "pt\"";
                                fontface = " style=\"font: " + font.getSize()+ "pt " + font.getName() + "\"";
                        } else {
                                fontface = "";
                        }
                        result ="<html><body" + fontface + ">" + this.linStagesHtml.get(this.linStagesHtml.size() - 1).toString() + "</body></html>";
                } else {
                        result = "";
                }
                return result;
        }
I

Metaleiro, obrigado pela ajuda, mas te confesso que não entendi o exemplo. Qual classe eles se referem? Verifiquei o JTextPane e o HTMLDocument e nenhuma delas tem os métodos chamados diretamente (por exemplo, getDOM()). Terias ai o link desses exemplos?

Obrigado

Criado 11 de julho de 2007
Ultima resposta 12 de jul. de 2007
Respostas 2
Participantes 2