Пример #1
0
  // counts lines
  public static int countLines(JTextArea textArea) {
    AttributedString text = new AttributedString(textArea.getText());
    FontRenderContext frc = textArea.getFontMetrics(textArea.getFont()).getFontRenderContext();

    int lines = 0;
    if (!textArea.getText().equals("")) {
      AttributedCharacterIterator charIt = text.getIterator();
      LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(charIt, frc);
      float formatWidth = (float) textArea.getSize().width;
      lineMeasurer.setPosition(charIt.getBeginIndex());
      while (lineMeasurer.getPosition() < charIt.getEndIndex()) {
        lineMeasurer.nextLayout(formatWidth);
        lines++;
      }
      for (int i = 0; i < textArea.getText().length(); i++) {
        if (textArea.getText().charAt(i) == '\r' || textArea.getText().charAt(i) == '\n') lines++;
      }
    } else {
      lines = 1;
    }
    return lines;
  }
Пример #2
0
 private static int getTextAreaBaseline(JTextArea text, int height) {
   Insets insets = text.getInsets();
   FontMetrics fm = text.getFontMetrics(text.getFont());
   return insets.top + fm.getAscent();
 }