Results 1 to 5 of 5
Thread: JEditorPane text disappears
- 01-10-2012, 10:33 PM #1
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
JEditorPane text disappears
I'm seeing something odd after I have set the character attributes in a styled document obtained from a JEditorPane: the string returned by getText() is missing some content.
When I run that code I get the following output:Java Code:import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.event.HyperlinkEvent; import javax.swing.event.HyperlinkListener; import javax.swing.text.Style; import javax.swing.text.html.HTMLDocument; public class GetTextStrangeness extends JPanel { private static final String EXAMPLE_TEXT = "<html><head><style>" + " a{text-decoration:none;color:black;}" + " a:hover{color:blue;text-decoration:underline;}" + "</style></head>" + "<body>" + " <p><a href='#foo'>Text for link foo</a>" + " <br/><a href='#bar'>Text for link bar</p>" + "</body></html>"; private JEditorPane ep; public GetTextStrangeness() { setLayout(new BorderLayout()); ep = new JEditorPane(); ep.setContentType("text/html"); ep.setEditable(false); ep.addHyperlinkListener(hoverListener); add(ep, BorderLayout.CENTER); JButton but = new JButton("Click me!"); but.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { System.out.println(ep.getText()); } }); add(but, BorderLayout.SOUTH); ep.setText(EXAMPLE_TEXT); System.out.println(ep.getText()); } private HyperlinkListener hoverListener = new HyperlinkListener() { @Override public void hyperlinkUpdate(HyperlinkEvent he) { HyperlinkEvent.EventType type = he.getEventType(); String styleName = null; if(type == HyperlinkEvent.EventType.ENTERED) { styleName = "a:hover"; } else if(type == HyperlinkEvent.EventType.EXITED) { styleName = "a"; } if(styleName != null) { HTMLDocument doc = (HTMLDocument)ep.getDocument(); Style newStyle = doc.getStyleSheet().getStyle(styleName); int start = he.getSourceElement().getStartOffset(); int end = he.getSourceElement().getEndOffset(); doc.setCharacterAttributes(start, end - start, newStyle, false); } } }; public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame("Test"); frame.add(new GetTextStrangeness()); frame.setSize(400, 300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } }
This is what I expect. When I move the mouse over the two lines of text I get the "hover" effect I want over the links. But after I have hovered over the links, clicking the button results in the following:Java Code:<html> <head> <style type="text/css"> <!-- a { color: black; text-decoration: none } a:hover { color: blue; text-decoration: underline } --> </style> </head> <body> <p> <a href="#foo">Text for link foo</a><br><a href="#bar">Text for link bar</a> </p> </body> </html>
The text content of the links has gone! (It's still visible in the JEditorPane, and still hovers as I intend.)Java Code:<html> <head> <style type="text/css"> <!-- a { color: black; text-decoration: none } a:hover { color: blue; text-decoration: underline } --> </style> </head> <body> <p> <font color="black"><a href="#foo"><a> </a></font><br> <font color="black"><a href="#bar"><a> </a></font> </p> </body> </html>
This is not causing me a problem because the pages are "static" and I can easily remember their text at the time I setText() on the editor pane. But I'm wondering if anybody knows what's going on. Partly to satisfy my curiosity and partly in case I am doing something wrong in the hypertext link listener that might come back to bite me later.
- 01-11-2012, 02:42 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: JEditorPane text disappears
For what its worth, the HTMLWriter class reproduces the result you report (with JRE 6):
...and it is my understanding (but don't quote me :) ) that this what getText() calls (indirectly). Perhaps check out the source to see what is going on under the hood:Java Code:StringWriter sw = new StringWriter(); HTMLWriter writer = new HTMLWriter(sw, (HTMLDocument)ep.getDocument()); writer.write();
javax.swing.text.html: HTMLWriter.javaLast edited by doWhile; 01-11-2012 at 02:44 AM.
- 01-13-2012, 02:39 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Re: JEditorPane text disappears
Sorry I didn't get back earlier to acknowledge your post, doWhile. I followed your link into the code (and lost a couple of hours of my life!) But I'm basically none the wiser. I think fundamentally I don't really understand how the whole model/view thing is implemented wrt html and JEditorPane in any detail.
It's no big problem though. My app does what I want, and I'm not so naive as to expect to make a silk purse out of a JEditorPane.
- 01-13-2012, 06:17 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: JEditorPane text disappears
No Worries. I sometimes get lost when diving into the java source as well...I was hoping the code in that link would let you play around with what getText returns - for instance you could override getText method to use the HTMLWriter class directly (as opposed to indirectly) and have it return the text you expect it to return. Not a big deal though as it seems you've figured a work around.
- 05-26-2012, 11:09 AM #5
Similar Threads
-
JEditorPane Text selecting and cursor position problems after scaling
By xlomo in forum New To JavaReplies: 0Last Post: 10-29-2011, 07:53 PM -
jDialog box disappears
By pink123 in forum AWT / SwingReplies: 3Last Post: 08-19-2011, 06:07 PM -
Getting text from inside a jeditorpane inside a jtabbedpane
By captain alge in forum New To JavaReplies: 9Last Post: 04-12-2011, 07:26 PM -
text box disappears
By okabeer in forum NetBeansReplies: 2Last Post: 07-13-2009, 06:21 PM -
Copy n' Paste in JEditorPane wraps text in new css..?
By Sam in forum AWT / SwingReplies: 0Last Post: 02-06-2008, 03:55 PM


LinkBack URL
About LinkBacks


Bookmarks