Reputation: 245
I'm displaying RSS feed in my JTextPanel. The result displayed doesn't go to a new line. How can I insert '\n' in a JTextPane? Thanks!
writeNews class:
public String writeNews()
{
String result = "";
try
{
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL u = new URL("http://thestar.com.my.feedsportal.com/c/33048/f/534600/index.rss");
Document doc = builder.parse(u.openStream());
NodeList nodes = doc.getElementsByTagName("item");
for(int i=0;i<nodes.getLength();i++)
{
Element element = (Element)nodes.item(i);
result += "\nTitle: " + getElementValue(element,"title");
result += "\nLink: " + getElementValue(element,"link");
result += "\nPublish Date: " + getElementValue(element,"pubDate");
result += "\nDescription: " + getElementValue(element,"description");
System.out.println("Title: " + getElementValue(element,"title"));
System.out.println("Link: " + getElementValue(element,"link"));
System.out.println("Publish Date: " + getElementValue(element,"pubDate"));
System.out.println("Description: " + getElementValue(element,"description"));
System.out.println();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
And the result are displayed on a simple JTextPane:
public void news()
{
news = new JPanel(new GridLayout());
news.setBackground(SystemColor.inactiveCaptionBorder);
JTextPane newsTextPane = new JTextPane();
newsTextPane.setContentType("text/html");
newsTextPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(newsTextPane);
news.add(scrollPane);
TextSamplerDemo reader = TextSamplerDemo.getInstance();
reader.writeNews();
String rssNews = reader.writeNews();
newsTextPane.setText(rssNews);
}
JTextPane View:
Console View:
Upvotes: 2
Views: 10824
Reputation: 1309
Buddies.. The way above 2 answers are working is logically correct but we should take help from built-in classes and functionality that java provides.
You may also procure the following way if you are to add other functionality similar to the questioned on
see http://docs.oracle.com/javase/7/docs/api/javax/swing/text/DefaultEditorKit.html
Upvotes: -1
Reputation: 12092
Here is a possible workaround,
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
public class TestLineBreak {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
sb.append("Text goes here <br>"); //<br> tag to insert line breaks
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane newsTextPane = new JTextPane();
newsTextPane.setContentType("text/html");
newsTextPane.setEditable(false);
newsTextPane.setText(sb.toString());
JScrollPane scrollPane = new JScrollPane(newsTextPane);
frame.add(scrollPane);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Upvotes: 3
Reputation: 44808
result += "Title: " + getElementValue(element,"title");
result += "Link: " + getElementValue(element,"link");
result += "Publish Date: " + getElementValue(element,"pubDate");
result += "Description: " + getElementValue(element,"description");
When you create your String
, you never use new lines. Either prepend the newline to the start of every line (other than the first line) or append the newline to the end of every line (other than the last line).
Upvotes: 2