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

document \n as TextArea line separator

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.2.0, 1.3.0
    • docs
    • beta
    • x86
    • windows_95, windows_nt



      Name: rm29839 Date: 05/29/98


      The separator between two lines in TextArea and
      JTextArea is "\n", not the line.separator property.
      This is fine but it needs to be documented.
      (Review ID: 30794)
      ======================================================================

      Name: skT45625 Date: 06/13/2000


      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      System.getProperty("line.separator") does not seem to work properly when applied
      to a JTextArea. The line separator for windows files is "\r\n" however this
      combination is not a good one for JTextArea because user can end up
      insertin text between the \r and the \n which can lead to false conclusions when
      examining the contents of the JTextArea. Run the attached code to demonstrate

      1. enter a few characters at the initial cursor position
      2. click the "Click Me" button to see what a buffered reader sees

      What appeared as a single line in the JTextArea is treated at two lines by the
      buffered reader. This is a major problem if you happen to be parsing the
      contents of a line because it does not appear to the user that anything is
      wrong.

      I use System.getProperty("line.separator") so that I can send strings to a
      JTextArea or to a file as I see fit, and would prefer not to develop a solution
      specific to the JTextArea. If the JTextArea is simply going to ignore \r, the
      why doesn't it strip them out? Is System.getProperty("line.separator") meant
      for use with files only? I have a feeling that you will call this a dupe of
      other bugs, yet I really think it is a problem that needs to be fixed. At a
      minimum, you should document the purpose of System.getProperty("line.separator")
      better.

      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;
      import java.io.*;

      public class NewLineTester extends JFrame {

         public JTextArea theArea = new JTextArea();
         
         public NewLineTester() {
            super("NewLineTest");
            
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            
            JPanel panel = new JPanel(new BorderLayout());
            
            JButton button = new JButton("Click Me");
            button.addActionListener(
               new ActionListener() {
                  public void actionPerformed(ActionEvent ev) {
                     BufferedReader br;
                     br = new BufferedReader(new StringReader(theArea.getText()));
                     String line;
                     StringBuffer sb = new StringBuffer();
                     try {
                        while ((line = br.readLine()) != null) {
                           sb.append(line + System.getProperty("line.separator"));
                        }
                     } catch (Exception ex) {
                     }
                     theArea.setText(sb.toString());
                  }
               }
            );
            
            panel.add(theArea, BorderLayout.CENTER);
            panel.add(button, BorderLayout.SOUTH);

            setSize(300, 200);
            setLocation(100, 100);
            setContentPane(panel);
            
         }
         
         public static void main(String args[]) {
            NewLineTester nlt = new NewLineTester();
            nlt.setVisible(true);
            
            String test = "type from the cursor, then click";
            nlt.theArea.setText(test +
                                System.getProperty("line.separator"));
            nlt.theArea.setCaretPosition(test.length() + 1);
         }
         
      }
      (Review ID: 106115)
      ======================================================================

            sharonz Sharon Zakhour (Inactive)
            rmandelsunw Ronan Mandel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: