Results 1 to 5 of 5
  1. #1
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

    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);
                }
            });
        }
    }
    When I run that code I get the following output:

    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>
    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>
          <font color="black"><a href="#foo"><a>
          </a></font><br>
          <font color="black"><a href="#bar"><a>
    </a></font>    </p>
      </body>
    </html>
    The text content of the links has gone! (It's still visible in the JEditorPane, and still hovers as I intend.)

    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.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: JEditorPane text disappears

    For what its worth, the HTMLWriter class reproduces the result you report (with JRE 6):
    Java Code:
    StringWriter sw = new StringWriter();
    HTMLWriter writer = new HTMLWriter(sw, (HTMLDocument)ep.getDocument());
    writer.write();
    ...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:

    javax.swing.text.html: HTMLWriter.java
    Last edited by doWhile; 01-11-2012 at 02:44 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: JEditorPane text disappears

    Quote Originally Posted by pbrockway2 View Post
    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.
    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.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: JEditorPane text disappears

    Removed spam, closing thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 10-29-2011, 07:53 PM
  2. jDialog box disappears
    By pink123 in forum AWT / Swing
    Replies: 3
    Last Post: 08-19-2011, 06:07 PM
  3. Replies: 9
    Last Post: 04-12-2011, 07:26 PM
  4. text box disappears
    By okabeer in forum NetBeans
    Replies: 2
    Last Post: 07-13-2009, 06:21 PM
  5. Replies: 0
    Last Post: 02-06-2008, 03:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •