1

I have a JTextPane I set its text with the following method.

public void setConfigPaneText(String content, Style style)
{
    StyledDocument logDoc = configPane.getStyledDocument();

    if(style == null)
    {
        style = configPane.addStyle("Style", null);
        StyleConstants.setForeground(style, Color.white);
        StyleConstants.setBold(style, true);
    }

    try 
    {
        configPane.setText(null);
        logDoc.insertString(logDoc.getLength(), content, style);
    }
    catch (BadLocationException e1)
    {
        e1.printStackTrace();
    }
}

I build the content String like this:

            if(f.exists())
            {
                Scanner scan = new Scanner(f);
                while(scan.hasNextLine()) 
                {
                    strbld.append(scan.nextLine()+"\n");
                }                   
                TopologyMain.nodes.get(i).setPtpConfig(strbld.toString()); // output
                scan.close();
            }

So I have this string appear in the JTextPane correctly, the problem is when I save the content of the JTextPane into a txt file and reload it to the JTextPane, one new empty row appears after every line.

Picture here: http://postimg.org/image/76z69oe7x/

Code doing save...

    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileChooser.getSelectedFile().getAbsolutePath())));
    out.print(configPane.getText());
    out.close()

and load:

if(filetmp.exists()) 
{
    Scanner scan;
    try 
    {
        scan = new Scanner(filetmp);

        while(scan.hasNextLine()) 
        {
            strbld.append(scan.nextLine()+"\n");
        }                   

        setConfigPaneText(strbld.toString(), null);

        scan.close();
    } 
    catch (FileNotFoundException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }

Without the /n in this method it looks like this: http://postimg.org/image/kn38ja8ov/

The cause of the problem can be that I have an additional "\r" character at the end of my lines as it can be seen here: http://postimg.org/image/9ny41rz3z/. But I do not know where they come from.

Thanks for your time!

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63

3 Answers3

3

The problem here is that you add the "\n" twice. Once as you build your content string and once where you load the file. If you delete the "\n" in your load function you should see the text without the additional empty line.

Deutro
  • 3,113
  • 4
  • 18
  • 26
  • I have already tried that, sorry for not mentioning in the question. The result without the /n in the load method : http://postimg.org/image/kn38ja8ov/ – Bence Kaulics Aug 19 '14 at 08:33
0

In your load function, you shouldn't add a newline for each scanned line, try this:

while(scan.hasNextLine()) 
{
  strbld.append(scan.nextLine());
} 
Gavi
  • 11
  • 2
  • Without that, there is no line breaking at all. – Bence Kaulics Aug 19 '14 at 08:37
  • Then I assume that the saved text file's newline characters has issues. Try to examine it with notepad++'s show all character function. Anyway, newlines can be added like this: http://stackoverflow.com/questions/14534767/how-to-append-a-newline-to-stringbuilder – Gavi Aug 19 '14 at 09:13
  • I mean strbld.append(System.getProperty("line.separator")); – Gavi Aug 19 '14 at 09:23
  • Yeah, I got it. This way of adding a line break has the same result. But the notepadd++ idea was useful, it turned out that I have an additional CR character. http://postimg.org/image/9ny41rz3z/ The question is now: why?. None of my load and save methods has "\r" in them. – Bence Kaulics Aug 19 '14 at 09:38
  • That is probably a windows feature. Afaik windows internally uses a \r\n for a new line. – Deutro Aug 19 '14 at 10:02
  • It was my fault, I have a method not shown in the question which gathers and parse information via an putty. In that method were the the additional CR characters. Thanks for all especially to Gavi. – Bence Kaulics Aug 19 '14 at 10:54
0

Solution from Gavi in the comments:

The problem's cause was that I had multiple CR ( "\r" ) characters as it can be seen here. You can check the output txt file with notepad++'s show all character function to find it out yourself. Then check up all code parts relate to the JTextPane or to the String you use as content to find the additional CRs and remove them.

Community
  • 1
  • 1
Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63