Class Main.TextLineNumber

All Implemented Interfaces:
ComponentListener, ImageObserver, MenuContainer, PropertyChangeListener, Serializable, EventListener, Accessible, CaretListener, DocumentListener
Enclosing class:
Main

public class Main.TextLineNumber extends JPanel implements CaretListener, DocumentListener, ComponentListener, PropertyChangeListener
This class will display line numbers for a related text component. The text component must use the same line height for each line. TextLineNumber supports wrapped lines and will highlight the line number of the current line in the text component. This class was designed to be used as a component added to the row header of a JScrollPane. Credits: The implementation of this class is based on "Text Component Line Number" Posted by Rob Camick on May 23, 2009 at https://tips4java.wordpress.com/2009/05/23/text-component-line-number/ Usage / License according to https://tips4java.wordpress.com/about/ : You are free to use and/or modify and/or distribute any or all code posted on the Java Tips Weblog without restriction. A credit in the code comments would be nice, but not in any way mandatory.
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • LEFT

      public static final float LEFT
      See Also:
    • CENTER

      public static final float CENTER
      See Also:
    • OUTER

      private final Border OUTER
    • component

      private JTextComponent component
    • borderGap

      private int borderGap
    • currentLineForeground

      private Color currentLineForeground
    • digitAlignment

      private float digitAlignment
    • minimumDisplayDigits

      private int minimumDisplayDigits
    • lastDigits

      private int lastDigits
    • lastLine

      private int lastLine
  • Constructor Details

    • TextLineNumber

      public TextLineNumber(JTextComponent component, int minimumDisplayDigits)
      Create a line number component for a text component.
      Parameters:
      component - the related text component
      minimumDisplayDigits - the number of digits used to calculate the minimum width of the component
  • Method Details

    • getBorderGap

      public int getBorderGap()
      Gets the border gap
      Returns:
      the border gap in pixels
    • setBorderGap

      public void setBorderGap(int borderGap)
      The border gap is used in calculating the left and right insets of the border. Default value is 5.
      Parameters:
      borderGap - the gap in pixels
    • getCurrentLineForeground

      public Color getCurrentLineForeground()
      Gets the current line rendering Color
      Returns:
      the Color used to render the current line number
    • setCurrentLineForeground

      public void setCurrentLineForeground(Color currentLineForeground)
      The Color used to render the current line digits. Default is Coolor.RED.
      Parameters:
      currentLineForeground - the Color used to render the current line
    • getDigitAlignment

      public float getDigitAlignment()
      Gets the digit alignment
      Returns:
      the alignment of the painted digits
    • setDigitAlignment

      public void setDigitAlignment(float digitAlignment)
      Specify the horizontal alignment of the digits within the component. Common values would be:
      • TextLineNumber.LEFT
      • TextLineNumber.CENTER
      • TextLineNumber.RIGHT (default)
    • getMinimumDisplayDigits

      public int getMinimumDisplayDigits()
      Gets the minimum display digits
      Returns:
      the minimum display digits
    • setMinimumDisplayDigits

      public void setMinimumDisplayDigits(int minimumDisplayDigits)
      Specify the minimum number of digits used to calculate the preferred width of the component.
      Parameters:
      minimumDisplayDigits - the number digits used in the preferred width calculation
    • setPreferredWidth

      private void setPreferredWidth()
      Calculate the width needed to display the maximum line number
    • paintComponent

      public void paintComponent(Graphics g)
      Draw the line numbers
      Overrides:
      paintComponent in class JComponent
    • isCurrentLine

      private boolean isCurrentLine(int rowStartOffset)
    • getTextLineNumber

      protected String getTextLineNumber(int rowStartOffset)
    • getOffsetX

      private int getOffsetX(int availableWidth, int stringWidth)
    • getOffsetY

      private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics) throws BadLocationException
      Throws:
      BadLocationException
    • caretUpdate

      public void caretUpdate(CaretEvent e)
      Specified by:
      caretUpdate in interface CaretListener
    • changedUpdate

      public void changedUpdate(DocumentEvent e)
      Specified by:
      changedUpdate in interface DocumentListener
    • insertUpdate

      public void insertUpdate(DocumentEvent e)
      Specified by:
      insertUpdate in interface DocumentListener
    • removeUpdate

      public void removeUpdate(DocumentEvent e)
      Specified by:
      removeUpdate in interface DocumentListener
    • documentChanged

      private void documentChanged()
    • propertyChange

      public void propertyChange(PropertyChangeEvent evt)
      Specified by:
      propertyChange in interface PropertyChangeListener
    • componentResized

      public void componentResized(ComponentEvent e)
      Specified by:
      componentResized in interface ComponentListener
    • componentMoved

      public void componentMoved(ComponentEvent e)
      Specified by:
      componentMoved in interface ComponentListener
    • componentShown

      public void componentShown(ComponentEvent e)
      Specified by:
      componentShown in interface ComponentListener
    • componentHidden

      public void componentHidden(ComponentEvent e)
      Specified by:
      componentHidden in interface ComponentListener