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

JTextPane jTextPane.setHighlighter(null) doesn't disable highlighter.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 5.0
    • 1.4.0, 1.4.2_03
    • client-libs
    • tiger
    • x86, sparc
    • solaris_1, windows_2000



      Name: ddT132432 Date: 11/28/2001


      d:\JBuilder\projects>java -version
      java version "1.3.1"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
      Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)


      Below is a swing applet that demonstrates problem -- on my machine.
      I set the highlighter for the textpane to null expecting no highlighting
      to occur when I click on text in the textpane.

      But clicking on a word in the text pane partially highlights the word -- on my
      machine. This error is also seen on JDk1.4beta3.

      Your documentation says:

      setHighlighter public void setHighlighter(Highlighter h) Sets the highlighter to
      be used. By default this will be set by the UI that gets installed. This can be
      changed to a custom highlighter if desired. The highlighter can be set to null
      to disable it. A PropertyChange event ("highlighter") is fired when a new
      highlighter is installed.


      package typeintest;

      import java.awt.*;
      import java.awt.event.*;
      import java.applet.*;
      import javax.swing.*;
      import javax.swing.text.*;
      import javax.swing.text.StyleConstants;

      public class TypeInTest extends JApplet
      {
          boolean isStandalone = false;
          JPanel jPanel1 = new JPanel();
          BorderLayout borderLayout1 = new BorderLayout();
          JTextPane jTextPane1 = new JTextPane();
          BorderLayout borderLayout2 = new BorderLayout();
          /**Get a parameter value*/
          public String getParameter(String key, String def)
          {
              return isStandalone ? System.getProperty(key, def) :
                  (getParameter(key) != null ? getParameter(key) : def);
          }

          /**Construct the applet*/
          public TypeInTest()
          {
          }
          /**Initialize the applet*/
          public void init()
          {
              try
              {
                  jbInit();
              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
          }
          /**Component initialization*/
          private void jbInit() throws Exception
          {
              this.setSize(new Dimension(400,300));
              this.getContentPane().setLayout(borderLayout1);
              jTextPane1.setEditable(false);
              jTextPane1.setCaretColor(Color.white);
              jTextPane1.setVerifyInputWhenFocusTarget(false);
              jTextPane1.setSelectedTextColor(Color.red);
              jTextPane1.setSelectionColor(Color.white);
              jTextPane1.setForeground(Color.white);
              jTextPane1.setText("jTextPane1");
              jTextPane1.addKeyListener(new TypeInTest_jTextPane1_keyAdapter(this));
              jTextPane1.addMouseListener(new TypeInTest_jTextPane1_mouseAdapter(this));
              jTextPane1.setHighlighter(null);
              jPanel1.setLayout(borderLayout2);
              jPanel1.setBorder(BorderFactory.createEtchedBorder());
              jPanel1.addKeyListener(new TypeInTest_jPanel1_keyAdapter(this));
              this.getContentPane().add(jPanel1, BorderLayout.CENTER);
              jPanel1.add(jTextPane1, BorderLayout.CENTER);
          }
          /**Start the applet*/
          private DefaultStyledDocument dsd = new DefaultStyledDocument();
      private SimpleAttributeSet attrs = new SimpleAttributeSet();
          private String textString = "Typein the missing word.";

          public void start()
          {
              try
              {

              StyleConstants.setFontFamily(attrs, "Serif");
      StyleConstants.setFontSize(attrs, 24);
              StyleConstants.setAlignment(attrs, StyleConstants.ALIGN_CENTER);
              StyleConstants.setBold(attrs, true);
              StyleConstants.setForeground(attrs, Color.blue);

              dsd.insertString(0, "\n" + textString + "\n\n", attrs);
              int length = dsd.getLength();
              dsd.insertString(length, textString, attrs);

              dsd.setParagraphAttributes(0, dsd.getLength(), attrs, false);
              jTextPane1.setDocument(dsd);
              Highlighter h = jTextPane1.getHighlighter();
              System.err.println("Highlighter = " + h.toString());
              }
              catch(Exception e)
              {
                  System.err.println("Exception: " + e.toString());
              }


          }
          /**Stop the applet*/
          public void stop()
          {
          }
          /**Destroy the applet*/
          public void destroy()
          {
          }
          /**Get Applet information*/
          public String getAppletInfo()
          {
              return "Applet Information";
          }
          /**Get parameter info*/
          public String[][] getParameterInfo()
          {
              return null;
          }
          /**Main method*/
          public static void main(String[] args)
          {
              TypeInTest applet = new TypeInTest();
              applet.isStandalone = true;
              JFrame frame = new JFrame();
              //EXIT_ON_CLOSE == 3
              frame.setDefaultCloseOperation(3);
              frame.setTitle("Applet Frame");
              frame.getContentPane().add(applet, BorderLayout.CENTER);
              applet.init();
              applet.start();
              frame.setSize(400,320);
              Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
              frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
              frame.setVisible(true);
          }

          //static initializer for setting look & feel
          static
          {
              try
              {
                  //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                 
      //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
              }
              catch(Exception e)
              {
              }
          }

          void jTextPane1_keyReleased(KeyEvent e)
          {
              System.err.println("Key Released");
          }

          void jTextPane1_mouseReleased(MouseEvent e)
          {
              //e.consume();
              System.err.println("Mouse Released");
              //jTextPane1.getHighlighter().removeAllHighlights();
          }

          void jPanel1_keyReleased(KeyEvent e)
          {
              System.err.println("Key Released");
          }

          void jTextPane1_keyPressed(KeyEvent e)
          {
              System.err.println("Key Pressed");
          }

          void jTextPane1_keyTyped(KeyEvent e)
          {
              System.err.println("Key Typed");
          }

          void jTextPane1_mouseEntered(MouseEvent e)
          {

          }

          void jTextPane1_mouseClicked(MouseEvent e)
          {
              //e.consume();
              System.err.println("Mouse Clicked");
                     // jTextPane1.getHighlighter().removeAllHighlights();
          }
      }

      class TypeInTest_jTextPane1_mouseAdapter extends java.awt.event.MouseAdapter
      {
          TypeInTest adaptee;

          TypeInTest_jTextPane1_mouseAdapter(TypeInTest adaptee)
          {
              this.adaptee = adaptee;
          }
          public void mouseReleased(MouseEvent e)
          {
              adaptee.jTextPane1_mouseReleased(e);
          }
          public void mouseEntered(MouseEvent e)
          {
              adaptee.jTextPane1_mouseEntered(e);
          }
          public void mouseClicked(MouseEvent e)
          {
              adaptee.jTextPane1_mouseClicked(e);
          }
      }

      class TypeInTest_jPanel1_keyAdapter extends java.awt.event.KeyAdapter
      {
          TypeInTest adaptee;

          TypeInTest_jPanel1_keyAdapter(TypeInTest adaptee)
          {
              this.adaptee = adaptee;
          }
          public void keyReleased(KeyEvent e)
          {
              adaptee.jPanel1_keyReleased(e);
          }
      }

      class TypeInTest_jTextPane1_keyAdapter extends java.awt.event.KeyAdapter
      {
          TypeInTest adaptee;

          TypeInTest_jTextPane1_keyAdapter(TypeInTest adaptee)
          {
              this.adaptee = adaptee;
          }
          public void keyPressed(KeyEvent e)
          {
              adaptee.jTextPane1_keyPressed(e);
          }
          public void keyReleased(KeyEvent e)
          {
              adaptee.jTextPane1_keyReleased(e);
          }
          public void keyTyped(KeyEvent e)
          {
              adaptee.jTextPane1_keyTyped(e);
          }
      }
      (Review ID: 136407)
      ======================================================================

            peterz Peter Zhelezniakov
            ddressersunw Daniel Dresser (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: