JTextPane does not display JScrollPane and does not wrap text

I need to display links, so I use JTextPane with setContentType. However, the content does not wrap and there is no scrolling. JTextPane content will be returned from the RSS feed. Here is the full code:

import java.awt.*;
import javax.swing.*;

class Main extends JFrame
{
    JFrame frame;
    JTabbedPane tabbedPane;
    JPanel home, news;      

    public Main()
    {
        setTitle("My Title" ); 
        setSize( 900, 600 ); 
        setLocationRelativeTo(null); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

        home(); 
        news(); 

        tabbedPane = new JTabbedPane(); 
        tabbedPane.addTab( " Home", home ); 
        tabbedPane.addTab( "News", news ); 

        JPanel framePanel = new JPanel();
        framePanel.setLayout(new BorderLayout());       
        framePanel.add( tabbedPane, BorderLayout.CENTER );
        getContentPane().add( framePanel );     

    }


    public void home() 
    {       
        home = new JPanel();
        // some stuffs here
    }


    public void news()
    {
        news = new JPanel();

        JTextPane newsTextPane = new JTextPane(); 
        newsTextPane.setContentType("text/html");
        newsTextPane.setEditable(false); 

        JScrollPane scrollPane = new JScrollPane(newsTextPane);     
        scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        news.add(scrollPane);   

        RSS reader = RSS .getInstance();
        reader.writeNews();             

        String rssNews = reader.writeNews();
        newsTextPane.setText(rssNews);
    }   

    public static void main( String args[] )
    {

        RSS reader = RSS.getInstance();
        reader.writeNews();

        Main mainFrame  = new Main();
        mainFrame.setVisible( true );   
        mainFrame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        }
}

My result: Screen shot

+5
source share
2 answers

I just used your code and this does not cause any problems:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities

public class TestScrolling {

    public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                 public void run() {
                  initUI();
                 });
    }

    public static void initUI() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 100; i++) {
            sb.append("loads loads loads loads of text here ");
        }
        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);
        scrollPane.setVerticalScrollBarPolicy(
                  javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        frame.add(scrollPane);
        frame.setSize(300, 200);
        frame.setVisible(true);
     }
}

EDIT:


You need to somehow force the width of the scrollPane. In my example, this is done implicitly by adding scrollpane to the frame content area, which by default uses BorderLayout. In your case, you used FlowLayout, which highlights the preferred scroll size, which is the preferred JTextPane size.

+6

- JScrollPane?

sscc @Guillaume Polet , :

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

public class TestScrolling {

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 100; i++) {
            sb.append("loads loads loads loads of text here ");

        }
        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);
        scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        JPanel pan = new JPanel();
        pan.setMinimumSize(new Dimension(500,500));
        pan.add(scrollPane);
        frame.add(pan);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}

, JscrollPane panel. /, , ?

0

All Articles