Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4186749

getLineCount() and line wrap: does not return current no of lines in JTextArea

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.1
    • client-libs



      Name: tb29552 Date: 11/03/98


      /*

      HI,
      I am using Swing-1.1beta3 and JDK 1.1.7.

      I have a JTextArea that is placed in a JScrollPane. My problem
      is that I cannot get the current number of lines from my
      JTextArea when wrapped lines are used. I try to use the
      getLineCount() method, but it only counts "\n" characters.

      The automatic new lines inserted due to the use of wrapped
      lines are not counted, Why?. It is only when I press "Carriage
      return" the lineCount is increased.

      In my case, I want to use wrapped lines so the user does not have
      to press "carriage return". I also want to know exactly the
      current number of lines (no matter how they where inserted).

      How can I find out how many lines the text
      was wrapped into? (this should equal the number
      of \n characters PLUS any extra wraps the JTextArea
      added.)

       */
      //
      import javax.swing.*;
      import java.awt.*;
      class WrapLineCount extends JFrame {
          static final String testText =
          "HI,\nI am using Swing-1.1beta3 " +
          "and JDK 1.1.7. I have a " +
          "JTextArea that is placed in a " +
          "JScrollPane. My problem is " +
          "that I cannot get the current " +
          "number of lines from my " +
          "JTextArea when wrapped lines " +
          "are used. I try to use the " +
          "getLineCount() method, but it " +
          "only reports \"1\" - the " +
          "automatic new lines inserted " +
          "due to the use of wrapped " +
          "lines are not counted, " +
          "Why?. It is only when I press " +
          "\"Carriage return\" the " +
          "lineCount is increased.\n In " +
          "my case, I want to use wrapped " +
          "lines so the user does not " +
          "have to press \"carriage " +
          "return\". I also want to know " +
          "exactly the current number of " +
          "lines (no matter how they " +
          "where inserted).\n This is the " +
          "java code for the creation of " +
          "my JScrollPane and my " +
          "JTextArea:";

          public static void outputMessage() {
              System.out.println("java.version = " +
                                 System.getProperty("java.version"));
              System.out.println("Operating System Name = " +
                                 System.getProperty("os.name"));
              System.out.println("Operating system architecture = " +
                                 System.getProperty("os.arch"));
              System.out.println("Operating system version = " +
                                 System.getProperty("os.version") + "\n");
          }

          WrapLineCount () {
              super();
              // Creation of JScrollPane
              JScrollPane connectedUserScrollPane = new JScrollPane();
              connectedUserScrollPane.setOpaque(true);
              connectedUserScrollPane.setHorizontalScrollBarPolicy
                  (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
              connectedUserScrollPane.setBounds(12, 24, 268, 241);
              connectedUserScrollPane.setFont(new Font("Dialog", Font.PLAIN, 12));
              connectedUserScrollPane.setForeground(new Color(0));
              connectedUserScrollPane.setBackground(new Color(-3355444));
              getContentPane().add(connectedUserScrollPane);

              // Creation of JTextArea

              JTextArea connectedUserTextArea = new JTextArea();
              connectedUserTextArea.setMargin(new java.awt.Insets(0, 0, 0, 0));
              connectedUserTextArea.setBounds(0, 0, 268, 241);
              connectedUserTextArea.setFont(new Font("Dialog", Font.PLAIN, 12));
              connectedUserTextArea.setForeground(new Color(0));
              connectedUserTextArea.setBackground(new Color(16777215));
              connectedUserTextArea.setLineWrap(true);
              connectedUserTextArea.setWrapStyleWord(true);
              connectedUserScrollPane.getViewport().add(connectedUserTextArea);

              int lines = connectedUserTextArea.getLineCount();
              System.out.println("Lines on empty: " + Integer.toString(lines));
              connectedUserTextArea.append(testText);
              System.out.println("Lines on 1: " +
                                 connectedUserTextArea.getLineCount());
              pack();
              setVisible(true);
          }
          public static void main(String[] args) {
              outputMessage();
              WrapLineCount wlc = new WrapLineCount ();
          }
      }

      (Review ID: 41948)
      ======================================================================

            tprinzing Tim Prinzing (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: