Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

java - Wrapping HTML text in a JEditorPane inside a JScrollPane

In an application, I'm using uneditable JEditorPanes as a sort of a generic UI widget that can display somewhat complex content (HTML will do the trick), wrap text lines and catch mouse clicks. Not sure if JEditorPane is a good choice for this, so feel free to suggest alternatives.

The following sample code works fairly well:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class Main {

  private static JPanel createPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    for (int i = 0; i < 3; i++) {
      JEditorPane editorPane = new JEditorPane();
      editorPane.setEditable(false);
      editorPane.setContentType("text/html");

      String text =
        "This is <b>item #" + i + "</b>." +
        " It's got text on it that should be wrapped."
      ;

      editorPane.setText(text);

      GridBagConstraints constraints = new GridBagConstraints();
      constraints.gridx = 0;
      constraints.gridy = i;
      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.weightx = 1.0;
      constraints.insets.bottom = 5;

      panel.add(editorPane, constraints);
    }

    return panel;
  }


  public static void main(String[] args) {
    JPanel panel = createPanel();

    JFrame frame = new JFrame();
    frame.setSize(200, 200);
    frame.setLocation(200, 200);

    // Change this to switch between examples
    boolean useScrollPane = false;

    if (useScrollPane) {
      JScrollPane scrollPane = new JScrollPane();
      scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
      scrollPane.setViewportView(panel);
      frame.add(scrollPane);
    }
    else {
      frame.add(panel);
    }

    frame.setVisible(true);
  }

}

and produces the following:

image

However, I may have a large number of these and could use a vertical scrollbar.

So I put the whole thing in a JScrollPane (change the useScrollPane variable to true in the sample code to see this version).

This gives me a vertical scrollbar if I shrink the window height, but the problem is that now the text is no longer wrapped:

image

So the question is: how can I get both the text wrapping and the vertical scrollbar?

As you can see, I disabled the horizontal scrollbar, but it didn't help much.

PS. I don't have much experience with Swing, so if you see some beginner WTFs in this code, please point them out :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
// JPanel panel = new JPanel();
ScrollablePanel panel = new ScrollablePanel();
panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );

See Scrollable Panel for the class and an explanation on how the class works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...