Simple enough: I have this SSCCE:
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import net.miginfocom.swing.MigLayout;
public class Test1_ChangeStyleAndAppend extends JFrame {
public class MyJTextPane extends JTextPane {
/**
* Append some text to this pane.
* @param s
*/
public void append(String s) {
try {
Document doc = this.getDocument();
doc.insertString(doc.getLength(), s, null);
} catch(BadLocationException e) {
System.err.println(e);
}
}
/**
* Append some text and change line.
* @param s
*/
public void appendLine(String s) {
try {
Document doc = this.getDocument();
doc.insertString(doc.getLength(), s + System.lineSeparator(), null);
} catch(BadLocationException e) {
System.err.println(e);
}
}
}
public Test1_ChangeStyleAndAppend() {
begin();
}
private void begin() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
MyJTextPane pane0 = new MyJTextPane();
pane0.appendLine("MyJTextPane using append() and then calling setText()");
pane0.appendLine("Second line. ");
pane0.appendLine("Third line");
pane0.setText(pane0.getText() + "At last" + System.lineSeparator());
pane0.setBorder(new EtchedBorder(EtchedBorder.RAISED));
add(pane0, BorderLayout.NORTH);
MyJTextPane pane = new MyJTextPane();
// changeLineSpacing(pane, 1.5f, false);
pane.appendLine("MyJTextPane calling appendLine()");
pane.appendLine("Second line. ");
pane.appendLine("Third line");
pane.appendLine("At last");
pane.setBorder(new EtchedBorder(EtchedBorder.RAISED));
add(pane, BorderLayout.CENTER);
JTextPane pane2 = new JTextPane();
pane2.setText("Normal JTextPane calling setText()");
pane2.setText(pane2.getText() + System.lineSeparator() + "Second line. ");
pane2.setText(pane2.getText() + System.lineSeparator() + "Third line");
pane2.setText(pane2.getText() + System.lineSeparator() + "At last");
pane2.setBorder(new EtchedBorder(EtchedBorder.RAISED));
add(pane2, BorderLayout.SOUTH);
pack();
setVisible(true);
}
/**
* Select all the text of a <code>JTextPane</code> first and then set the line spacing.
* @param pane the <code>JTextPane</code> to apply the change
* @param factor the factor of line spacing. For example, <code>1.0f</code>.
* @param replace whether the new <code>AttributeSet</code> should replace the old set. If set to <code>false</code>, will merge with the old one.
*/
public static void changeLineSpacing(JTextPane pane, float factor, boolean replace) {
pane.selectAll();
MutableAttributeSet set = new SimpleAttributeSet(pane.getParagraphAttributes());
StyleConstants.setLineSpacing(set, factor);
pane.setParagraphAttributes(set, replace);
pane.setCaretPosition(0); //scroll to the top.
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Test1_ChangeStyleAndAppend frame = new Test1_ChangeStyleAndAppend();
}
});
}
}
The result is:
I am with Windows.
I have two TextPanes, one is JTextPane
, and another is my class extending JTextPane
and in this one I have defined two convenient methods, append()
and appendLine()
in my custom class, but I haven't overwritten super getText()
method.
Now, when I call appendLine()
and then call setText()
in my class, the calling of getText()
adds another \r
between lines, making it \r\r\n
and add additional empty lines between two lines. It's not what I want, I don't want \r
to be added.
I had this variable of type JTextArea
and for changing line spacing, I changed it to be JTextPane
because JTextArea
cannot have adjustable line spacing. But it occurs all over the project and all the time it used pane.setText(pane.getText + newText)
. Now I have to search all occurrence of setText()
and change it to appendLine()
. Before doing this big change I want to understand why this extra \r
is added.
If I always call append()
or always call setText(pane.getText() + newText)
, it doesn't happen. But if I call append()
first and then call setText(pane.getText() + newText)
, \r
is added.
Someone sheds light on this?
The line separator for Windows is "\r\n".
The Document only uses "\n" for the new line string.
Not sure exactly what the problem is but somewhere there is an inconsistency on how the newline string is handled between the insertString(...) and getText() methods.
The simple solution is to update the Document with its newline string, not the platform newline string.
//doc.insertString(doc.getLength(), s + System.lineSeparator(), null);
doc.insertString(doc.getLength(), s + "\n", null);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments