JTextPane does not go to a new line?

℡╲_俬逩灬. 提交于 2019-12-10 23:46:49

问题


I'm displaying RSS feed in my JTextPanel. Result displayed doesn't go to a new line. How can I insert \n in 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:


回答1:


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);
    }
} 



回答2:


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).




回答3:


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



来源:https://stackoverflow.com/questions/10467605/jtextpane-does-not-go-to-a-new-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!