Auf Thema antworten

Hallo Forum,


ich möchte einen formatierten html-Text als String speichern, so dass die Formatierungsinformationen erhalten bleiben.


Zum Testen habe ich die zwei JEditorpPanes epQuelle und epZiel erstellt. In der Quelle formatiere ich den Text (funktioniert), in Zeile 38 packe ich ihn in einen String, um ihn dann ins epZiel einzufügen. Der Text erscheint dort, allerdings unformatiert. Offensichtlich werden die Formatierungs-Tags mittels doc.getText verschluckt. Ich habe es auch mit epQuelle.getText und epQuelle.getDocument().getText versucht. In Zeile 43 habe ich auch epZiel. setText probiert. Das hat alles nichts gebracht.


Was mache ich hier falsch? :oops:


[code=Java]public static void test() {


    // Vorbereitung

    HTMLEditorKit html = new HTMLEditorKit();

    SimpleAttributeSet atts = new SimpleAttributeSet();

    epQuelle.setContentType("text/html");

    epQuelle.setEditorKit(html);

    epZiel.setContentType("text/html");

    epZiel.setEditorKit(html);

    StyledDocument doc = (StyledDocument) epQuelle.getDocument();


    // Text in Quelle einfuegen

    try {

        epQuelle.getDocument().insertString(0, "fett kursiv unterstrichen", null);

    } catch (Exception e) {

        System.out.println("Fehler" + e);

    }


    // Formatierungen

    epQuelle.setSelectionStart(0);

    epQuelle.setSelectionEnd(4);

    StyleConstants.setBold(atts, true);

    doc.setCharacterAttributes(epQuelle.getSelectionStart(), epQuelle.getSelectionEnd() - epQuelle.getSelectionStart(), atts, false); // fett

    epQuelle.setSelectionStart(5);

    epQuelle.setSelectionEnd(12);

    StyleConstants.setBold(atts, false);

    StyleConstants.setItalic(atts, true);

    doc.setCharacterAttributes(epQuelle.getSelectionStart(), epQuelle.getSelectionEnd() - epQuelle.getSelectionStart(), atts, false); // kursiv

    epQuelle.setSelectionStart(12);

    epQuelle.setSelectionEnd(25);

    StyleConstants.setItalic(atts, true);

    StyleConstants.setUnderline(atts, true);

    doc.setCharacterAttributes(epQuelle.getSelectionStart(), epQuelle.getSelectionEnd() - epQuelle.getSelectionStart(), atts, false); // unterstrichen


    // Text in String speichern und einfuegen

    String htmlText = "";

    try {

        htmlText = doc.getText(0, doc.getLength());

    } catch (Exception e) {

        System.out.println("Fehler" + e);

    }

    try {

        epZiel.getDocument().insertString(0, htmlText, null);

    } catch (Exception e) {

        System.out.println("Fehler " + e);

    }

}[/code]



Oben