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.