Here you can find the source of paintUnderline(Graphics g, JTextComponent tc, int pos0, int pos1)
public static void paintUnderline(Graphics g, JTextComponent tc, int pos0, int pos1) throws BadLocationException
//package com.java2s; /*// w ww . j a v a 2 s.co m Copyright (C) 2005 Eduardo Jodas Samper This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA author e-mail: eduardj@dev.java.net */ import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.plaf.TextUI; import javax.swing.text.BadLocationException; import javax.swing.text.JTextComponent; public class Main { public static void paintUnderline(Graphics g, JTextComponent tc, int pos0, int pos1) throws BadLocationException { g.setColor(Color.RED); TextUI ui = tc.getUI(); Rectangle r = ui.modelToView(tc, pos1 + 1); int h = r.y + r.height - 2; int max = r.x - 2; int current = ui.modelToView(tc, pos0).x; while (current <= max) { g.drawLine(current, h, Math.min(current + 2, max), h + 1); current += 3; if (current <= max) { g.drawLine(current, h, Math.min(current + 2, max), h); current += 3; } } } }