Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6927469

Hyperlinks enclosed in <p> tags in JEditorPanes do not behave as expected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 7
    • client-libs

      OPERATING SYSTEM
      ----------------
      All

      FULL JDK VERSION
      ----------------
      All (reproducible with 1.4.2, 5.0, 6 and 7)

      PROBLEM DESCRIPTION
      -------------------
      Under certain circumstances a hyperlink in a JEditorPane can be opened by clicking in blank space near the link, rather than on the link. This even happens when the mouse pointer does not indicate that clicking will open the link, so it is counter-intuitive.

      The problem occurs only when a hyperlink is the first item in a HTML paragraph. It does not occur if the link is not the first content contained within a set of <p> tags, for example:

      "First link as para <p>extra text<a href='http://www.google.com/&#39;&gt;Para&lt;/a&gt;&lt;/p> "

      or if it is not enclosed in <p> tags at all:

      "First link as para <a href='http://www.google.com/&#39;&gt;Para&lt;/a>"

      REPRODUCTION INSTRUCTIONS
      -------------------------
      1. Compile and run the attached testcase
      2. Observe a fram containing a line of text, and a hyperlink below it.
      3. Click the empty space between the text and hyperlink. Observe that
         the link is opened, even though the link itself was not clicked.

      TESTCASE
      --------

      import java.awt.BorderLayout;
      import java.io.IOException;
      import javax.swing.JEditorPane;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.event.HyperlinkEvent;
      import javax.swing.event.HyperlinkListener;

      public class HrefPossibleBug {
          public static void main(String[] argv) {
              //Editorpane
              JEditorPane pane = new JEditorPane("text/html", "First link as para <p><a href='http://www.google.com/&#39;&gt;Para&lt;/a&gt;&lt;/p> ");
              pane.setEditable(false);
              pane.setOpaque(false);
              pane.addHyperlinkListener(new HyperlinkListener() {
                  public void hyperlinkUpdate(HyperlinkEvent event) {
                      if (HyperlinkEvent.EventType.ACTIVATED.equals(event.getEventType())) {
                          System.out.println(event.getURL());
                          try {
                              Runtime.getRuntime().exec("cmd.exe /C start http://www.google.com ");
                          } catch (IOException ioe) {
                              System.out.println(ioe.getMessage());
                          }
                      }
                  }
              });

              //Panel
              JPanel p = new JPanel();
              p.add(pane);

              //Frame
              JFrame f = new JFrame("HyperlinkPossibleBug");
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.getContentPane().add(p, BorderLayout.CENTER);
              f.setSize(150, 150);
              f.setVisible(true);
          }
      }

            peterz Peter Zhelezniakov
            dkorbel David Korbel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: