JDK-6448694 : html table using percentage widths is incorrect
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-07-13
  • Updated: 2011-01-31
  • Resolved: 2006-07-14
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
J2SE 6.0 beta 2

ADDITIONAL OS VERSION INFORMATION :
windows xp sp 2
mac ox 10.4.7

A DESCRIPTION OF THE PROBLEM :
When creating a table using <table width=100%>, and columns using a specified percentage width, the column size is calculated incorrectly, and teh columns widths are not the erquested percentage.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run the supplied test case under both 1.4.2 and 1.5 or 1.6

resize the window, notice how under 1.4.2 the columns remain aligned (the table is 100% and the  colums are 20% and 80%), under the later versions the column widths vary based on content. Actually, what I think is happeneding is that tha


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Each column should remain at its requested percentage width regardless of content.


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package tyler.empire.ui;

import java.awt.Color;

import javax.swing.*;
import javax.swing.border.LineBorder;

/**
* test using a html table with % column widths
*/
public class TestHtmlTableWidths {
      public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable(){
           public void run() {
               new TestFrame();
           }});
   }

   static class TestFrame extends JFrame {
              public TestFrame() {
           JTextPane p1 = new JTextPane();
           JTextPane p2 = new JTextPane();
           JTextPane p3 = new JTextPane();
           JTextPane p4 = new JTextPane();
                      p1.setBorder(new LineBorder(Color.RED));
           p2.setBorder(new LineBorder(Color.RED));
           p3.setBorder(new LineBorder(Color.RED));
           p4.setBorder(new LineBorder(Color.RED));
                      p1.setContentType("text/html");
           p2.setContentType("text/html");
           p3.setContentType("text/html");
           p4.setContentType("text/html");
                      p1.setText("<html><table width=100%><tr><td width=80%>This is some text</td><td width=20% align=left>column #2</td></tr></table>");
           p2.setText("<html><table width=100%><tr><td width=80%>smaller</td><td width=20% align=left>this is some much larger text in column #2</td></tr></table>");
           p3.setText("<html><table width=100%>"+
                   "<tr><td width=80%>This is some text</td><td width=20% align=left>column #2</td></tr>"+
                   "<tr><td width=80%>smaller</td><td width=20% align=left>this is some much larger text in column #2</td></tr>"+
                   "</table>");
           p4.setText("<html><table width=100%><tr><td width=80%>smaller</td><td width=20% align=left>this is really a lot of text in column #2 but it should still align the column with the component above</td></tr></table>");
                                 getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
                      getContentPane().add(p1);
           getContentPane().add(p2);
           getContentPane().add(p3);
           getContentPane().add(p4);
                      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      pack();
           show();
       }
   }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
None.

I think the offending code is ColumnIterator.getMinimumSpan() in javax.swing.text.html.TableView

In 1.4.2, the method was:

	public float getMinimumSpan(float parentSpan) {
	    if ((percentages != null) && (percentages[col] != 0)) {
		return Math.max(percentages[col], columnRequirements[col].minimum);
	    }
	    return columnRequirements[col].minimum;
	}

In 1.5 the method is

	public float getMinimumSpan(float parentSpan) {
	    // do not care for percentages, since min span can't
            // be less than columnRequirements[col].minimum,
	    // but can be less than percentage value.
	    return columnRequirements[col].minimum;
	}

I made the changes in a local copy and it worked correctly.

Comments
EVALUATION This bug is fixed indirectly as a result of earlier fix for "6306218: JEditorPane is unable to do reasonably spaced indentation in complex HTML tables". At the time this bug was evaluated, it already became "Not a Defect".
21-07-2006

EVALUATION > Each column should remain at its requested percentage width regardless of content. This is incorrect. We do not want to render columns narrower than the content requires. This was on of the problems addressed by 6306218 [JEditorPane is unable to do reasonably spaced indentation in complex HTML tables]. By that fix I wanted to follow what other renders do You can try this in IE or mozilla. Our rendering looks very close. <html> <body> <table border=1 width=100%> <tr><td width=80%>This is some text</td><td width=20% align=left>column #2</td></tr> </table> <table border=1 width=100%> <tr><td width=80%>This is some text</td><td width=20% align=left>column #2</td></tr> <tr><td width=80%>smaller</td><td width=20% align=left>this is some much larger text in column #2</td></tr> </table> <table border=1 width=100%> <tr><td width=80%>smaller</td><td width=20% align=left>this is really a lot of text in column #2 but it should still align the column with the component above</td></tr> </table> </body> </html> In your case the second column of the last table is wider than the second columns of other tables. It happens because it contains the widest word - 'component'. We can not maker column narrower than that and yet place 'component' inside it. Before the fix for 6306218 [JEditorPane is unable to do reasonably spaced indentation in complex HTML tables] we could break a word on character boundaries. Thus one could end up with word 'component' split across two lines. Something like that: comp onent If you want to break word on character boundaries you might want to use trick like this: com<b></b>po</b>nent to hint the renderer where to break the word. closing this bug as "not a defect"
14-07-2006