5

I need to display links so I'm using JTextPane with setContentType. However, the content doesn't wrap and there's no scroll. The content of JTextPane will be returned from a RSS feed. Here's 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: Screenshot

Lily S
  • 245
  • 1
  • 8
  • 18

2 Answers2

6

I just used your code and it 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 have to force somehow the width of the scrollPane. In my example it is done implicitly by adding the scrollpane to the content pane of the frame, which by default uses the BorderLayout. In your case, you used a FlowLayout which allocates the preferred size of the scrollpane which is about the preferred size of the JTextPane.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • +1 for [sscce](http://sscce.org/); I'm guessing the question's (unseen) [markup](http://stackoverflow.com/q/2645834/230513) and [container](http://stackoverflow.com/q/4702891/230513) may be also be a factor. – trashgod May 05 '12 at 11:30
  • @Guillaume Polet +1 for the example, I used it to show that with wrong container size the scrollbar wont appear. The OP is using a `panel` but we don't know enough about it. – alain.janinm May 05 '12 at 11:46
  • @trashgod, I've included the whole code, please advice! thanks! – Lily S May 05 '12 at 12:58
  • 3
    @LilyT in the news JPanel, you don't set any layout, meaning that you use the FlowLayout (Manager) which will always allocate the preferredSize. Use a BorderLayout instead and everything will work fine: `news = new JPanel(new BorderLayout());` – Guillaume Polet May 05 '12 at 13:14
  • 1
    I was going to say `news = new JPanel(new GridLayout())`, but it's a similar effect. See also [Initial Threads](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod May 05 '12 at 13:31
  • @trashgod You mean that I should rather initiate the UI in the EDT? I know I should, this was just out of lazyness. Will edit that, because it's true that we often see users create GUI as I just did and this should not be repeated. – Guillaume Polet May 05 '12 at 13:33
  • Thanks Guillaume Polet! It's fine now! :) – Lily S May 05 '12 at 13:37
  • @GuillaumePolet: Sorry, a minor nit; I meant to address future readers. – trashgod May 05 '12 at 13:42
  • @trashgod Good point; you are right to make me do that change. The answer is "more correct" this way. – Guillaume Polet May 05 '12 at 13:49
0

Are you using a panel or something around your JScrollPane?

Taking the sscc of @Guillaume Polet with innapropriate size the example won't work :

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

I see your adding your JscrollPane to panel. Can you provide the creation/modification you made on that panel and where this panel is used?

alain.janinm
  • 19,951
  • 10
  • 65
  • 112