Results 1 to 4 of 4
  1. #1
    Jack_Maloney is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default How to underline text using a different colour

    Hi,

    I want to be able to underline some text in a different colour to that which the text is in and wondered if anyone knew of a way to do this without using CSS.

    This is the code that I have at present:

    Java Code:
    private void underlineText(int offset, int length, Color color) {		
    		SimpleAttributeSet attrs = new SimpleAttributeSet();
    		attrs.addAttribute(StyleConstants.Underline, Boolean.TRUE);
    		super.setCharacterAttributes(offset, length, attrs, false);
    }
    Please let me know if there is an easy way to do this which I am missing...

    Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: How to underline text using a different colour

    Moving to AWT/Swing.

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

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: How to underline text using a different colour

    I don't see why you pass a color parameter to that method, seeing as it's not used.

    You could try using a custom Highlighter.HighlightPainter by extending DefaultHighlighter.DefaultHighlightPainter and overriding paintLayer(...) to paint only the bottom thickness pixels of the highlight. Crude example without bounds checking for the thickness:
    Java Code:
    import java.awt.*;
    import javax.swing.text.*;
    
    class UnderlineHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
      
      private int thickness;
    
      public UnderlineHighlightPainter(Color c, int thickness) {
        super(c);
        this.thickness = thickness;
      }
    
      // Copied from DefaultHighlighter.DefaultHighlightPainter
      // Lines 419 to 459
      @Override
      public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds,
              JTextComponent c, View view) {
        Color color = getColor();
    
        if (color == null) {
          g.setColor(c.getSelectionColor());
        } else {
          g.setColor(color);
        }
        if (offs0 == view.getStartOffset()
                && offs1 == view.getEndOffset()) {
          Rectangle alloc;
          if (bounds instanceof Rectangle) {
            alloc = (Rectangle) bounds;
          } else {
            alloc = bounds.getBounds();
          }
          //g.fillRect(alloc.x, alloc.y, alloc.width, alloc.height);
          g.fillRect(alloc.x, alloc.y + alloc.height - thickness, alloc.width, thickness);
          return alloc;
        } else {
          try {
            Shape shape = view.modelToView(offs0, Position.Bias.Forward,
                    offs1, Position.Bias.Backward,
                    bounds);
            Rectangle r = (shape instanceof Rectangle)
                    ? (Rectangle) shape : shape.getBounds();
            //g.fillRect(r.x, r.y, r.width, r.height);
            g.fillRect(r.x, r.y + r.height - thickness, r.width, thickness);
            g.setColor(color);
    
            return r;
          } catch (BadLocationException e) {
          }
        }
        return null;
      }
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: How to underline text using a different colour

    Now if I'd known you weren't really interested, I wouldn't have bothered.

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

Similar Threads

  1. Change colour of JOptionPane
    By chirag123 in forum AWT / Swing
    Replies: 1
    Last Post: 05-03-2011, 03:48 AM
  2. Help with Apace POI in setting underline
    By nn12 in forum New To Java
    Replies: 0
    Last Post: 01-22-2011, 07:58 AM
  3. changing JFrame colour
    By simo_mon in forum AWT / Swing
    Replies: 0
    Last Post: 08-08-2009, 03:15 AM
  4. Changing text colour in code
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-29-2009, 04:32 PM
  5. Replies: 0
    Last Post: 03-26-2009, 02:23 AM

Posting Permissions

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