import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class LineNumber extends JComponent { static final long serialVersionUID = 3679449495322768618L; private final static Color DEFAULT_BACKGROUND = new Color(230, 163, 4); private final static Color DEFAULT_FOREGROUND = Color.black; private final static Font DEFAULT_FONT = new Font("monospaced", Font.PLAIN, 12); // LineNumber height (abends when I use MAX_VALUE) private final static int HEIGHT = Integer.MAX_VALUE - 1000000; // Set right/left margin private final static int MARGIN = 5; // Line height of this LineNumber component private int lineHeight; // Line height of this LineNumber component private int fontLineHeight; private int currentRowWidth; // Metrics of this LineNumber component private FontMetrics fontMetrics; /** * Convenience constructor for Text Components */ public LineNumber(JComponent component) { if (component == null) { setBackground(DEFAULT_BACKGROUND); setForeground(DEFAULT_FOREGROUND); setFont(DEFAULT_FONT); } else { setBackground(DEFAULT_BACKGROUND); setForeground(component.getForeground()); setFont(component.getFont()); } setPreferredSize(9999); } public void setPreferredSize(int row) { int width = fontMetrics.stringWidth(String.valueOf(row)); if (currentRowWidth < width) { currentRowWidth = width; setPreferredSize(new Dimension(2 * MARGIN + width, HEIGHT)); } } public void setFont(Font font) { super.setFont(font); fontMetrics = getFontMetrics(getFont()); fontLineHeight = fontMetrics.getHeight(); } /** * The line height defaults to the line height of the font for this * component. The line height can be overridden by setting it to a positive * non-zero value. */ public int getLineHeight() { if (lineHeight == 0) return fontLineHeight; else return lineHeight; } public void setLineHeight(int lineHeight) { if (lineHeight > 0) this.lineHeight = lineHeight; } public int getStartOffset() { return 4; } public void paintComponent(Graphics g) { int lineHeight = getLineHeight(); int startOffset =