Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Limit horizontal size of JScrollPane view

843807Dec 2 2009 — edited Dec 2 2009
Hi folks, I've got an issue here that (despite my best search efforts) I can't seem to find a solution to. I think it might be related to this thread: [http://forums.sun.com/thread.jspa?forumID=57&threadID=5222131]

Basically, I've got a JTextPane within a JPanel within a JScrollPane, and my issue is that I want the JTextPane to wrap, but it's not.

Here's an SSCCE (minus imports):
public class ST1 {
   public static void main(String[] args) {
      //general frame setup
      JFrame frame = new JFrame("ST1");
      frame.setSize(400, 400);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      //generate text
      StringBuffer text = new StringBuffer("<html>");
      for ( int i = 0; i < 1000; i++ ) text.append("This text is really long. ");
      
      //textPane
      JTextPane textPane = new JTextPane();
      textPane.putClientProperty(JTextPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
      textPane.setEditable(false);
      textPane.setContentType("text/html");
      textPane.setText(text.toString());
      
      //add with another component to a details pane
      JPanel detailsPane = new JPanel(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints(GridBagConstraints.RELATIVE, 
            0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 
            new Insets(0, 0, 0, 0), 0, 0);
      detailsPane.add(new JLabel("Some Label: "), gbc);
      detailsPane.add(textPane, gbc);
      
      //scroll the details pane
      JScrollPane scrollPane = new JScrollPane(detailsPane, 
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      frame.add(scrollPane);
      
      frame.setVisible(true);
   }
}
Now I think I understand that what's happening is the "detailsPane's" preferred size is as wide as it needs to be to accommodate the JTextPane without wrapping. That part doesn't surprise me. However, I was hoping by specifying a "never" horizontal scrollbar policy, the "scrollPane" would lay out the "detailsPane" such that the widths would match, thus causing "textPane" to wrap. But that's not how it works, apparently.

Does anybody have a solution to this, or a link to where I could read more? I didn't find anything after a scan of the forums or the Java Tutorial for JScrollPane, though I feel like this must be an easy and common situation.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 30 2009
Added on Dec 2 2009
9 comments
1,410 views