If JTextComponent contains a child Component, and that child Components size changes, JTextComponent doesn't pick up the new size.
This worked in 1.3.1
###@###.### 2002-07-01
Java version:
java version "1.4.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_01-b03)
Java HotSpot(TM) Client VM (build 1.4.0_01-b03, mixed mode)
OS: Windows 2K Professional
I have a question regarding insertion of JLabels into a JTextPane. I'm
building an application which allows insertion of JLabels, rendering
HTML, into a JTextPane by using JTextPane's insertComponent() method.
Upon insertion, the label will be sized inappropriately small.
However, if something is typed directly after the label, then the label
will be resized correctly. This problem will manifest itself again
when the JTextPane is resized. How can I make the label be sized
correctly from the beginning when it is inserted, and also when the
JTextPane is resized? I've attached a simple example program which
demonstrates this issue. The main method is in Application1.java.
<attached Application1.java, Frame1.java>.
Workaround Suggested:
use htmlLabel.setPreferredSize(new java.awt.Dimension(500,83));
Customer feedback:
As for your suggestion about calling setPreferredSize() on JLabel, I
don't think setting a hard-coded preferred size will be the optimal
solution. These labels will contain strings whose content and lengths
are specified by the end user, so I am unable to predict exactly what
preferred size should be set each time a JLabel is created and inserted
into the JTextPane.
Another problem with this solution is if I shrink the width of the
frame containing the JTextPane, then the text in the label will be word
wrapped and the label's height will grow until it hits the 83 pixel
preferred height limit. At this point, the label will no longer grow,
and the text in the label will be cropped again.
What is it about typing something immediately adjacent to the label,
which causes the label to be resized correctly? If we can find this out, then
that would go a long way towards solving my problem.
----------
Based on BugId: 4348815, I suggested customer to implement the suggestion
mentioned in the evaluation. Please see below the comments:
I read the RFE, but the suggested solution ( by overriding JLabel's
getPreferredSize() ) does not work when the JLabel is embedded in a JTextPane.
The height of the label still does not adjust according to the set width.
I was playing around with other containers, and if you embed a JLabel
rendering HTML inside a JPanel, then the JLabel actually sizes itself
appropriately when it is inserted and when the parent JPanel is resized.
(I've attached an example program). So what does JPanel do differently from
JTextPane when hosting JLabels? Perhaps this might be another way to address
my issue. <attached Application2.java, Frame2.java>