Hi!
I have a JTextPane which width need to be adjusted related of the
width of an other component. I have a component listener that informs
me that the other component is resized, so I have the new width.
The textpane has inside a lot of component inserted and need to be
resized depending the height of the components. This is works with the
following solution by recalculate the height after each component
insertion.
public void recalculateEditorHeight() {
// calculate the preferred width and size for the component after the
// component is inserted
int preferredWidth = textPane.getPreferredSize().width;
View v = textPane.getUI().getRootView(textPane);
v.setSize(preferredWidth, Integer.MAX_VALUE);
int preferredHeight = (int)v.getPreferredSpan(View.Y_AXIS);
// set the calculated preferred size
textPane.setPreferredSize(new Dimension(preferredWidth,
preferredHeight));
}
The problem is, if I change the width of the JTextPane (related to the
width change of the other component) the following method to calc the
height of the inserted components is not working. Always give back the
last calculated height.
I think when resizing the width of the component need to do something
with the View as well, but I have no successful result yet (tried to
set the size of the view, tried to revalidate, ...).
Have anybody some ideea, how to resize the JTextPane width, to get
afterwards the height calculation method to work?
Thanks,
Lalo
Thomas Weidenfeller - 17 May 2005 09:27 GMT
> I have a JTextPane which width need to be adjusted related of the
> width of an other component.
Use layout managers instead of calculating the layout by yourself.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
lalo - 17 May 2005 18:08 GMT
In the meantime I found the solution (it was a bug?). Unfortunatelly I
resized the parent object and not the JTextPane directly. The parent
object was a JPanel, so I thought that if I change the preferredSize of
the panel, than the text pane will resize correctly. It's size was
resized, but this height calculation never worked after that.
Thomas, thanks for your hint, but I am in a situation where I need to
resize a JTree node width manually, to be fit into the containing
panel. The tree nodes are panels that contains JTextPanes.
Unfortunatelly there are no layout managers that extends automatically
the nodes width.