Для чего это стоило, у меня есть старый (некрасивый) код, когда я пытался манипулировать HTML-текстом в документе.
Я использовал методы, отличные от insertString(...)
.Может быть, это даст вам некоторые идеи?
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class TextPaneHTML extends JFrame implements ActionListener
{
JTextPane textPane;
HTMLEditorKit editorKit;
HTMLDocument doc;
Element root;
public TextPaneHTML()
{
textPane = new JTextPane();
// textPane.setEditable( false );
textPane.setContentType( "text/html" );
textPane.setEditable(false);
editorKit = (HTMLEditorKit)textPane.getEditorKit();
doc = (HTMLDocument)textPane.getDocument();
root = doc.getDefaultRootElement();
DefaultCaret caret = (DefaultCaret)textPane.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
textPane.setText( "<html><body>Hello world I want some long text <a href=\"http://answers.polldaddy.com/poll/1742928/\">Do you like polls</a> to it wraps to a new line World!</body></html>" );
// textPane.setCaretPosition(7);
// textPane.setText( "" );
// printInfo();
// Add text pane to frame
JScrollPane scrollPane = new JScrollPane( textPane );
scrollPane.setPreferredSize( new Dimension( 200, 200 ) );
getContentPane().add( scrollPane );
// Add a append button
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout(1, 0) );
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
createButton("Normal", buttonPanel);
createButton("Styled", buttonPanel);
createButton("B1", buttonPanel);
createButton("B2", buttonPanel);
createButton("B3", buttonPanel);
try
{
HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
while (it.isValid())
{
SimpleAttributeSet s = (SimpleAttributeSet)it.getAttributes();
System.out.println(s);
s.removeAttribute(HTML.Attribute.HREF);
System.out.println( s.getClass().getName());
String href = (String)s.getAttribute(HTML.Attribute.HREF);
int start = it.getStartOffset();
int end = it.getEndOffset();
String text = doc.getText(start, end - start);
System.out.println( href + " : " + text );
it.next();
}
}
catch(Exception e) {};
}
public void createButton(String text, JPanel panel)
{
JButton button = new JButton(text);
button.addActionListener( this );
panel.add( button );
}
public void actionPerformed(ActionEvent ae)
{
String command = ae.getActionCommand();
String text;
try
{
if ("Normal".equals( command ))
{
text = "normal text";
editorKit.insertHTML(doc, doc.getLength(), text, 0, 0, null);
}
if ("Styled".equals( command ))
{
// text = "<font size=5 color=red>font and color test</font>";
text = "<a href=\"abc\">hyperlink</a>";
editorKit.insertHTML(doc, doc.getLength(), text, 0, 0, null);
// doc.insertString(doc.getLength(), text, null);
// try {
// editorKit.read(new StringReader(text), doc, doc.getLength());
// } catch (Exception e) {
// e.printStackTrace();
// }
}
if ("B1".equals( command ))
{
// text = "<br>B1";
text = "<a href=\"abc\">hyperlink</a>";
Element element = root.getElement(1);
doc.insertBeforeEnd(element, text);
}
if ("B2".equals( command ))
{
Element element = root.getElement(1);
text = "<br>B2-after start";
doc.insertAfterStart(element, text);
// why doesn't this work
text = "<br>B2-before end";
doc.insertBeforeEnd(element, text);
}
if ("B3".equals( command ))
{
// text = "<p>B3</p>";
// editorKit.insertHTML(doc, textPane.getCaretPosition(), text, 0, 0, HTML.Tag.P);
text = "<a href=\"abc\">hyperlink</a>";
editorKit.insertHTML(doc, textPane.getCaretPosition(), text, 0, 0, HTML.Tag.A);
// doc.insertString(doc.getLength(), text, null);
}
}
catch(Exception e)
{
// BadLocationException ble = (BadLocationException)e;
// System.out.println(e + " : " + ble.offsetRequested());
System.out.println(e);
}
// System.out.println( command + ": -----------" );
// System.out.println( "Elements: " + root.getElementCount() );
// System.out.println( root.getElement(0).getAttributes() );
// System.out.println( root.getElement(1).getAttributes() );
try
{
System.out.println( "textPane: " + textPane.getText() );
System.out.println( "Document: " + textPane.getDocument().getText(0, textPane.getDocument().getLength()) );
}
catch(Exception e2) {}
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
TextPaneHTML frame = new TextPaneHTML();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setSize(50, 120);
frame.setVisible(true);
}
});
}
}