on an XP platform I display a file's text in a JTextArea. The API clearly says under DefaultEditorKit that line ends are handled as \n. But it also says that the EndOfLineStringProperty takes care for platform differences. My example code below shows that when text is inserted at a line end, it goes right in between the CR and the LF which gives funny results when the textarea's contents is written back to a file.
Is this intended behavior of the textArea? And what is the recommended way to display and write files with CR+LF line terminators?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclass InsertAtLineEnd
{
publicstaticvoid main(String args[])
{
JFrame frame = new JFrame("InsertAtLineEnd");
frame.setSize(350, 300);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container cp= frame.getContentPane();
cp.setLayout(null);
final JTextArea ta= new JTextArea();
ta.setBounds(40,40,250,110);
ta.setFont(new Font("Monospaced", Font.PLAIN, 12));
ta.setText(" These are some sample lines\r\n"+
"as read from a file on a windows\r\n"+
"platform.\r\n Place the cursor at any line\r\n"+
"end and insert one or more\r\n"+
"characters. Then press the button.\r\n");
JButton b= new JButton("Get text");
b.setBounds(105,200,80,30);
b.setMargin(new Insets(0,0,0,0));
b.addActionListener(new ActionListener()
{ publicvoid actionPerformed(ActionEvent e)
{ String s=ta.getText();
System.out.println(s);
for (int i=0; i<s.length(); i++) {
System.out.println(s.charAt(i)+" "+(int)s.charAt(i));
}
}
});
cp.add(ta);
cp.add(b);
frame.setVisible(true);
}
}
When you use the EditorKit's read() method to read text from a file and store it in the JTextArea, it converts any DOS "\r\n" or Mac "\r" line separators to "\n". The same thing happens when you use the built-in Paste command to paste text from the clipboard. But text you insert using the setText() or append() commands, or Document's insertString() or replace() methods, doesn't get filtered like that. The idea is that you just use "\n", and don't worry about what kind of line separators the file really uses.
thank you for your reply. So we can say: Displaying files with CR+LF line terminators looks fine. Even modifiying the text in the middle or at beginning of a line is no problem when writing the text back to a file. But in case of modification at a line end, terminators have to be replaced.
The Document uses "\n" to represent the new line character.
When using the read() method the external new line string ("\r\n", "\n", "\r"), depending on the file being read, will be saved and will be converted to a "\n" when added to the Document.
When using the write(...) method the "\n" string will be converted back to the external new line string.
That is why it is always better to use the read() and write() methods of JTextComponent, than it is to create your own BufferedReader and BufferedWriter to read and write files. When using the latter approach you lose the original new line string information.
Thanks for your explanation. My problem is only that I sometimes need to append a file to the textArea and read() can fill the area only from scratch. So at least in that case I have to do the conversion myself. With write(), however, there is no problem.
Re: JTextArea, inserting text at end of line
Jan 29, 2008 1:55 PM
(reply 5
of 6) (In reply to
#4 )
Correct
sometimes need to append a file to the textArea
I have never tried it, but the DefaultEditorKit has a read(...) method that allows you to specify the insertion point into the Document. I assume it would parse out the new lines strings the same as the original read() method would.