Elliotd123
Elliotd123

Reputation: 1

JTextPane and newlines

I'm writing a program that (at one point) makes a command-line call to another native application, gets the output from that application, and puts it into a JTextPane as a String. The problem is, it doesn't seem to grab the newline characters the way it should. Because I'm using linux, each line ends with a ^M instead of a \n.

Is there any way to tell Java to look for those and create a newline in the string?

private void getSettings() {
    Commander cmd = new Commander();
    settings = cmd.getCommandOutput("hdhomerun_config " + ipAddress + " get /sys/boot");
    settingsTextPane.setText(settings);
}

I end up with the output barfed into one line and wrapped around in the text pane.

Upvotes: 0

Views: 720

Answers (2)

Elliotd123
Elliotd123

Reputation: 1

Thanks guys, I looked through my code again and realized I was reading the output from the program one line at a time, and just appending the lines. I needed to add a \n at the end of each line that I read. Correct me if I'm wrong, but I believe Java automatically corrects newlines based on your operating system.

Upvotes: 0

A4L
A4L

Reputation: 17595

As I recall Unix displays ^M for the carriage return character \r so you could try to replace it by using the replace method of the String class

settingsTextPane.setText(settings.replace('\r', '\n'));

Upvotes: 1

Related Questions