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

JEditorPane MouseEvent issue with frame-containing HTML pages

XMLWordPrintable

      FULL PRODUCT VERSION :
      Java 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
      I believe it does still occur in Java SE 6.0

      ADDITIONAL OS VERSION INFORMATION :
      Windows Version 5.2.3790

      A DESCRIPTION OF THE PROBLEM :
      I have a program that contains a simple web browser implemented with JEditorPane that uses MouseEvents to cause the loading of hyperlinks from the page. When an HTML page is loaded (using the setPage(URL x) method) that doesn't contain any frames, mouse events are generated correctly on clicking on the editor pane. However when an HTML document that contains frames is loaded mouse events longer seem to be generated (the apropriate method in my listener-implementing class is never called). Subsequently loading a frame-free page makes the mouse events work correctly again. It is also worth noting that some other events such as key events also don't work, but hyperlink events do continue to work correctly.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      If you run the above program, you will notice that if you have no page
      loaded or a frame-free page loaded (e.g. type
      http://www.google.co.uk into the url box and click "Go") then when you
      click anywhere in the editor pane the mouse events occur and the
      field increments. However when you load a page with frames (such as
      http://java.sun.com/javase/6/docs/api/) then the mouse events stop
      working properly (the field no longer increments, i.e. the events are
      not being fired).


      ---------- BEGIN SOURCE ----------
      Here is a short example that exhibits the bug.

      /*
       * Main.java
       *
       * Created on 04 April 2007, 12:50
       *
       * To change this template, choose Tools | Template Manager
       * and open the template in the editor.
       */

      package javabug;

      /**
       *
       * @author Tom
       */
      public class Main {
         
          /**
           * Creates a new instance of Main
           */
          public Main() {
          }
         
          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              // TODO code application logic here
              Browser browser = new Browser();
              browser.main(args);
          }
         
      }



      /*
       * Browser.java
       *
       * Created on 04 April 2007, 12:51
       */

      package javabug;

      import java.net.*;

      /**
       *
       * @author Tom
       */
      public class Browser extends javax.swing.JFrame {
         
          URL hyperlinkURL = null;
          int mouseEventCounter = 0;
         
          /**
           * Creates new form Browser
           */
          public Browser() {
             
              this.setTitle("Browser");
             
              initComponents();
             
              this.editorPane.setEditable(false);
              this.editorPane.validate();
             
              this.mouseEventsLabel.setText(""+this.mouseEventCounter);
              this.mouseEventsLabel.validate();
          }
         
          /** This method is called from within the constructor to
           * initialize the form.
           * WARNING: Do NOT modify this code. The content of this method is
           * always regenerated by the Form Editor.
           */
         
          /*### NETBEANS GUI GENERATION STUFF
           *
           * Just standard stuff and a editor pane. There is an action
      listener looking at the goButton,
           * and a hyperlink listener and a mouse released event listener
      looking at the editor pane.
           *
           */
          // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
          private void initComponents() {
              jLabel1 = new javax.swing.JLabel();
              urlTextField = new javax.swing.JTextField();
              goButton = new javax.swing.JButton();
              jScrollPane1 = new javax.swing.JScrollPane();
              editorPane = new javax.swing.JEditorPane();
              jLabel2 = new javax.swing.JLabel();
              hyperlinkLabel = new javax.swing.JLabel();
              jLabel3 = new javax.swing.JLabel();
              mouseEventsLabel = new javax.swing.JLabel();

              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
              jLabel1.setText("URL");

              urlTextField.setText("http://");

              goButton.setText("Go");
              goButton.addActionListener(new java.awt.event.ActionListener() {
                  public void actionPerformed(java.awt.event.ActionEvent evt) {
                      goButtonActionPerformed(evt);
                  }
              });

              editorPane.addHyperlinkListener(new
      javax.swing.event.HyperlinkListener() {
                  public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent
      evt) {
                      editorPaneHyperlinkUpdate(evt);
                  }
              });
              editorPane.addMouseListener(new java.awt.event.MouseAdapter() {
                  public void mouseReleased(java.awt.event.MouseEvent evt) {
                      editorPaneMouseReleased(evt);
                  }
              });

              jScrollPane1.setViewportView(editorPane);

              jLabel2.setText("Hyperlink:");

              jLabel3.setText("Mouse Events:");

              org.jdesktop.layout.GroupLayout layout = new
      org.jdesktop.layout.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                  
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                  .add(org.jdesktop.layout.GroupLayout.TRAILING,
      layout.createSequentialGroup()
                      .addContainerGap()
                      
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                          .add(org.jdesktop.layout.GroupLayout.LEADING,
      jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 715,
      Short.MAX_VALUE)
                          .add(org.jdesktop.layout.GroupLayout.LEADING,
      layout.createSequentialGroup()
                              .add(jLabel1)
                              
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                              .add(urlTextField,
      org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
                              
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                              .add(goButton))
                          .add(org.jdesktop.layout.GroupLayout.LEADING,
      layout.createSequentialGroup()
                              .add(jLabel2)
                              
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                              .add(hyperlinkLabel))
                          .add(org.jdesktop.layout.GroupLayout.LEADING,
      layout.createSequentialGroup()
                              .add(jLabel3)
                              
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                              .add(mouseEventsLabel)))
                      .addContainerGap())
              );
              layout.setVerticalGroup(
                  
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                  .add(layout.createSequentialGroup()
                      .addContainerGap()
                      
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                          .add(jLabel1)
                          .add(goButton)
                          .add(urlTextField,
      org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
      org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
      org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                      .add(jScrollPane1,
      org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                      
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                          .add(jLabel3)
                          .add(mouseEventsLabel))
                      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                      
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                          .add(jLabel2)
                          .add(hyperlinkLabel))
                      .addContainerGap())
              );
              pack();
          }// </editor-fold>

         
          /*## Here is the mouse released event listener that stops working
      when the page contains a frame ##*/
         
          private void editorPaneMouseReleased(java.awt.event.MouseEvent evt) {

              // inc the mouse event counter
              this.mouseEventCounter++;
              this.mouseEventsLabel.setText(""+this.mouseEventCounter);
             
              // load the clicked on page
              if(this.hyperlinkURL != null) {
                  try {
                      // load page
                      this.editorPane.setPage(this.hyperlinkURL);
                      // reset the hyperlink caption thing
                      this.hyperlinkURL = null;
                      this.hyperlinkLabel.setText("");
                      this.hyperlinkLabel.validate();
                  } catch (Exception e) {
                      this.editorPane.setText("Exception occurred while
      loading " + this.urlTextField.getText() + "\n" + e.toString());
                  }
                  this.editorPane.validate();
              }
          }

          /*## This hyperlink event listener keep working, whether there are
      frames or not ##*/
         
          private void
      editorPaneHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
             
              // Keep track of the target url pointed at by the mouse
              if(this.hyperlinkURL == null) { // new hyperlink, generally
      happens when mouse on
                  this.hyperlinkLabel.setText(evt.getURL().toExternalForm());
                  this.hyperlinkLabel.validate();
                  this.hyperlinkURL = evt.getURL();
              } else {
                  if(this.hyperlinkURL.sameFile(evt.getURL())) { // mouse off
                      this.hyperlinkLabel.setText("");
                      this.hyperlinkLabel.validate();
                      this.hyperlinkURL = null;
                  } else { // directly some other link
                      this.hyperlinkLabel.setText(evt.getURL().toExternalForm());
                      this.hyperlinkLabel.validate();
                      this.hyperlinkURL = evt.getURL();
                  }
              }
          }

          /* This is just used to load the page */
          private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {
      // TODO add your handling code here:
              try {
                  this.editorPane.setPage(this.urlTextField.getText());
              } catch (Exception e) {
                  this.editorPane.setText("Exception occurred while loading "
      + this.urlTextField.getText() + "\n" + e.toString());
              }
              this.editorPane.validate();
          }
         
         
         
          /**
           * @param args the command line arguments
           */
          public static void main(String args[]) {
              java.awt.EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      new Browser().setVisible(true);
                  }
              });
          }
         
          // Variables declaration - do not modify
          private javax.swing.JEditorPane editorPane;
          private javax.swing.JButton goButton;
          private javax.swing.JLabel hyperlinkLabel;
          private javax.swing.JLabel jLabel1;
          private javax.swing.JLabel jLabel2;
          private javax.swing.JLabel jLabel3;
          private javax.swing.JScrollPane jScrollPane1;
          private javax.swing.JLabel mouseEventsLabel;
          private javax.swing.JTextField urlTextField;
          // End of variables declaration
         
      }
      ---------- END SOURCE ----------

      REPRODUCIBILITY :
      This bug can be reproduced always.

            peterz Peter Zhelezniakov
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: