JDK-4700671 : 1.4 REGRESSION: JTextComponent doesn't update visual size of children Components
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_7
  • CPU: sparc
  • Submitted: 2002-06-11
  • Updated: 2002-12-06
  • Resolved: 2002-10-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other Other Other
1.4.0_03 03Fixed 1.4.1_02Fixed 1.4.2Fixed
Description
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>


Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.4.0_03 1.4.1_02 mantis mantis-beta FIXED IN: 1.4.0_03 1.4.1_02 mantis mantis-beta INTEGRATED IN: 1.4.0_03 1.4.1_02 mantis mantis-b10 mantis-beta
08-07-2004

WORK AROUND Remove the Component and add it back, or set a custom RepaintManager that knows about Components that are the children of a JTextComponent and does the following: RepaintManager.setCurrentManager(new RepaintManager() { private java.util.List cvs = new ArrayList(); private java.util.List tmp = new ArrayList(); public synchronized void addInvalidComponent( JComponent invalidComponent) { if (invalidComponent == l) { cvs.add(l); } super.addInvalidComponent(invalidComponent); } public void validateInvalidComponents() { synchronized(this) { tmp.addAll(cvs); } for (int counter = tmp.size() - 1; counter >= 0; counter--) { ((Component)tmp.get(counter)).getParent().validate(); } tmp.clear(); super.validateInvalidComponents(); } }); Modul null pointer checking and all that good stuff...
08-07-2004

EVALUATION There are two problems in Invalidator: . What is in validate should be calling child.getXXX vs super.getxxx() . What is validate (modulo the isValid check) should be in doLayout. ###@###.### 2002-06-11 Needs to be fixed in hopper ###@###.### 2002-08-29
11-06-2002