JDK-4292511 : JTableHeader height determined by first column given HTML text
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0,1.4.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic,windows_98,windows_nt
  • CPU: generic,x86
  • Submitted: 1999-11-17
  • Updated: 2017-05-23
Related Reports
Relates :  
Relates :  
Description
Name: krT82822			Date: 11/17/99


Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)

JDK1.3 behaves differently than 1.2.2 with regards to HTML headers containing
"<p>" to create multi-line headers.  1.3 only looks to the first header when
determining height.

Although I've submitted this as a bug, I can understand the performance
implications, and suggest you just document this change.

import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;

public class HTMLHeader extends JFrame {
  public HTMLHeader() {
    getContentPane().add( new JScrollPane( new JTable( new TableData() ) ) );
  }

  class TableData extends AbstractTableModel {
    private String [] headers = {
      // Add &nbsp;<p> before Centered to enable 2-line header.
      "<html><center>Centered Heading</center></html>",
      "<html><center>Centered Multi-Line Column<p>Heading</center></html>",
    };
    public String getColumnName(int column) {
      return headers[ column ];
    }
    public int getColumnCount() {
      return 2;
    }
    public int getRowCount() {
      return 1;
    }
    public Object getValueAt(int rowIndex, int columnIndex) {
      return "Cell";
    }
  }

  public static void main( String [] args ) {
    HTMLHeader test = new HTMLHeader();
    test.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
    test.addWindowListener( new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit( 0 );
      }
    } );
    test.pack();
    test.setVisible( true );
  }
}
(Review ID: 97996) 
======================================================================

Name: krT82822			Date: 02/15/2000


orig synopsis:  "JTable headers using html don't display correctly"
15 Feb 2000 eval1127@eng -- tried the workaround for bug # 4292511...but it's still a problem in kestrel-rc1.  This problem is really a dupe of 4292511.

for win98:

java version "1.3.0rc1"
java(TM) 2 Runtime Environment, Standard Edition (built 1.3.0rc1-T)
java HotSpot(TM) Client VM (build 1.3.0rc1-S mixed mode)

and (for linux):

java version "1.2.2-RC2"
Classic VM (build 1.2.2-RC2-K, green threads, javacomp)


In JDK 1.2.2 the following program displays the headers with wrapped text,
but the header is only one line long (cropping most of the text) until it is
resized.  The text should show up correctly the first time and size correctly
as it is displayed.

In JDK 1.3, the text is never wrapped.  What is worse is that there is
never any indication that text is missing (whole words that can't be
rendered completely just are cut off).  Try sizing the column headers
to see this.  Cell elements at least use an ellipses to indicate that
some text is truncated.  This might be a good idea for html headers as well.
The worst here is that HTML headers containing a line break (<br>) in JDK 1.3
will never be displayed correctly.

/*
This program demonstrates that there is a problem with the interaction
between the HTML renderering and the JTable Headers.  When the table
with these headers comes up, the headers are rendered on two lines, but
only one line is visible.  The first time the table is resized (by a user)
the headers show better, the second time, they are perfect.  Something similiar
happens when resizing a large table to a smaller size makes the headers go
from one line to two.

This may be a general problem with HTML rendering.  How does the renderer
know to break lines unless it knows the current width (or height).  It seems
like one of those would be needed to decide how to render.
*/

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;

public class TableTest {
    static String[] columnNames = {
        "<html><b><font color=black>Testing</font></b>",
        "<html><b><font color=black>Shown Today</font></b>",
        "<html><b><font color=black>This is a test of a large label to see if it
wraps </font></b>",
        "<html><b><font color=black>t1</font></b>"
    } ;
    static Object[][] rowData = {{"1", "2", "3", "4"}, {"1", "2", "3", "this is
a test" }};
    
    public static void main(String[] args)
    {
        JFrame f = new JFrame("table test");
        JTable table = new JTable(rowData, columnNames);

        table.setPreferredScrollableViewportSize(new Dimension(400, 300));

        JScrollPane scrollPane = new JScrollPane(table);
        f.getContentPane().add(scrollPane, BorderLayout.CENTER);

        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        f.pack();
        f.setVisible(true);
        
    }
}
(Review ID: 101195)
======================================================================

Comments
EVALUATION Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?messageID=12058&forumID=1463
16-03-2006

EVALUATION Name: apR10133 Date: 07/20/2001 We have to document this according to evaluation of 4351785 and BasicTableHeaderUI.getHeaderHeight() comments. ###@###.### ======================================================================
25-09-2004

WORK AROUND Name: krT82822 Date: 11/17/99 Add the appropriate number of &nbsp;<p> text to the first column header to get the required table header height. ====================================================================== Name: krT82822 Date: 02/15/2000 For JDK 1.2.2: Use <br> to force line breaks For JDK 1.3: possibly override the default cell renderer to include a larger height (I haven't tried this yet) (Review ID: 101195) ======================================================================
25-09-2004