Swing - My class extends JTextPane and calling first my append() and then super.setText() add additional line break

WesternGun

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:

enter image description here

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?

camickr

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Super() not working on my extends class

From Dev

Super() not working on my extends class

From Dev

Why does adding a super-class break my Spring beans?

From Dev

JTextPane line break

From Dev

JTextPane line break

From Dev

How do I add a line break in my Twilio SMS message?

From Dev

Calling overloaded super constructor in ScalaJS class that extends a native class

From Dev

Clear memory of parent class instance created by calling super/extends

From Dev

How to make JTextPane line break

From Dev

How do I add a line break / blank line after my LOG statement?

From Dev

How to add action bar to my main activity from another class that extends ActionBarActivity?

From Dev

How can I find, recursively, all files in my directory tree that first character in first line of each file is a space, a tab or a line break?

From Dev

Calling methods for my java class

From Dev

How to add or append a parameter for class which extends Action.Simple

From Dev

How to add additional id on my javascript?

From Dev

Adding & Removing additional class on my element tag

From Dev

How can I add an additional line break (paragraph return) to each line break found in an Open Office Document?

From Dev

How can I add a line break between my inline-block javascript divs?

From Dev

How can I add a line break between my inline-block javascript divs?

From Dev

Why isn't my derived class method calling the overriden method from base class despite using the super keyword?

From Dev

Have system not call keyPressed in my class that extends class that implements KeyListener

From Dev

sphinx add a page break in my pdf with latexpdf?

From Dev

should i add data items directly to my adapter or pass them to my class first?

From Dev

super keyword without extends to the super class

From Dev

super keyword without extends to the super class

From Dev

How to break text from one line to another line in my list

From Dev

Why does calling focus() break my CSS transition?

From Dev

cannot implements with the onBindViewHolder in my class extends RecyclerView.Adapter

From Dev

Adding a scrollbar to my JTextArea- class extends JFrame

Related Related

  1. 1

    Super() not working on my extends class

  2. 2

    Super() not working on my extends class

  3. 3

    Why does adding a super-class break my Spring beans?

  4. 4

    JTextPane line break

  5. 5

    JTextPane line break

  6. 6

    How do I add a line break in my Twilio SMS message?

  7. 7

    Calling overloaded super constructor in ScalaJS class that extends a native class

  8. 8

    Clear memory of parent class instance created by calling super/extends

  9. 9

    How to make JTextPane line break

  10. 10

    How do I add a line break / blank line after my LOG statement?

  11. 11

    How to add action bar to my main activity from another class that extends ActionBarActivity?

  12. 12

    How can I find, recursively, all files in my directory tree that first character in first line of each file is a space, a tab or a line break?

  13. 13

    Calling methods for my java class

  14. 14

    How to add or append a parameter for class which extends Action.Simple

  15. 15

    How to add additional id on my javascript?

  16. 16

    Adding & Removing additional class on my element tag

  17. 17

    How can I add an additional line break (paragraph return) to each line break found in an Open Office Document?

  18. 18

    How can I add a line break between my inline-block javascript divs?

  19. 19

    How can I add a line break between my inline-block javascript divs?

  20. 20

    Why isn't my derived class method calling the overriden method from base class despite using the super keyword?

  21. 21

    Have system not call keyPressed in my class that extends class that implements KeyListener

  22. 22

    sphinx add a page break in my pdf with latexpdf?

  23. 23

    should i add data items directly to my adapter or pass them to my class first?

  24. 24

    super keyword without extends to the super class

  25. 25

    super keyword without extends to the super class

  26. 26

    How to break text from one line to another line in my list

  27. 27

    Why does calling focus() break my CSS transition?

  28. 28

    cannot implements with the onBindViewHolder in my class extends RecyclerView.Adapter

  29. 29

    Adding a scrollbar to my JTextArea- class extends JFrame

HotTag

Archive