JTextPane doesn't display JScrollPane and doesn't Wrap Text

感情迁移 提交于 2019-11-28 13:42:32

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.

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?

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