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

The underline in underlined text should be a separate object.

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs



      Name: jl125535 Date: 12/05/2001


      No JVM data given.

      If the underline in underlined text was a separate and accessable object rather
      than a Font attribute, the more sophisticated text editors/renderers could be
      developed for end users.

      For example look at MS Word. Misspelled words are underlined with a sawtooth
      red line that is a different color than the text.

      I myself am developing an educational app/applet that draws text and (could) use
      different color and shaped underlines to represent differerent things. For
      example: a black underline might mean "Important" a yellow underline might mean
      "More Important" and a red underline might mean "Critical Information"

      And again, different underline characteristics could mean different things.
      For example: in a hyperlinked text page, a sinusoidal underline might mean a
      sound link and while a dot-dash underline might mean a graph of a sound.

      Hopefully, you can easily see the utility of such a mechanism.


      The following code shows the limitations the current underlining scheme.
      There is a lot of code, so do a search on "underline".

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

      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();

          // THIS IS THE TEXT STRING I USE BELOW
          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();
                  /*Here I set the underline attribute for the next string I
                  * insert. It has to be the same color as the
                  * text string. There is no way to change the color of the underline
                  * without changing the color of the text
                  */
                  StyleConstants.setUnderline(attrs, true);
                  dsd.insertString(length, textString, attrs);

                  /*Here I try to set the underline color to red
                  * but the color of the text also changes
                  */
                  StyleConstants.setForeground(attrs, Color.red);
                  StyleConstants.setUnderline(attrs, true);
                  dsd.setCharacterAttributes(12,7,attrs,true);

                   jTextPane1.setDocument(dsd);

                  /* I was looking at trying to replace the DefaultHighligher with my own
                  * Highlighter that would draw an underline with attributes such as color
                  * and shape. But that looks a little too complicated and I loose highlighting
                  * But having an Underliner interface and a DefaultUnderliner class integrated
                  * into the JTextPane similar to the way the Highlighter interface is implemented
                  * by the DefaultHighlighter class would be a good idea -- I think. Then
                  * a programmer could copy and change the way the implementation draws underlines
                  * and/or set the necessary characteristics by somehow setting an underline drawing
                  * method.
                  *
                  DefaultHighlighter d =
                         (DefaultHighlighter)jTextPane1.getHighlighter();
                  System.err.println("Highlighter = " + d.toString());
                  TypeinTestHighlighter tth = new TypeinTestHighlighter();
                  jTextPane1.setHighlighter(tth);
                  TypeinTestHighlighter t = (TypeinTestHighlighter)jTextPane1.getHighlighter();
                  System.err.println("Highlighter = " + t.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);
          }

          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: 136537)
      ======================================================================

            idk Igor Kushnirskiy (Inactive)
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: