JDK-4351785 : MultiLine JTable Header stopped working in 1.3
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2000-07-10
  • Updated: 2000-07-13
  • Resolved: 2000-07-13
Related Reports
Relates :  
Description

Name: jk109818			Date: 07/10/2000


java full version "1.3.0rc3-Z"

I have been using a JTable with a "MultilineHeaderRendere as described in
Chapter 19 of
Graphic Java 2, Volume 2 by David M. Geary.

This allow a table header to grow to multiple crows as the column gets resized.
JDK 1.2
This worked fine, but in 1.3 it stopped working.  Any ideas why?



Thanks for you Help.

Here is the sample code from the book.  IN JDK 1.2 if you resize the last
column, the table header will always display the entire header (taking up as
many rows as needed.  In JDK 1.3, the header gets clipped as the column shrinks.

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

public class Test extends JFrame {
	String longTitle = "Last Name\nMaiden Name (if divorced)";
	MultilineHeaderRenderer multilineRenderer =
						new
MultilineHeaderRenderer(longTitle);

	JTable table = new JTable(
		new Object[][] {
			{ "Lynn", "M.", "Seckinger" },
			{ "Carol", "R.", "Seckinger" },
			{ "Roy", "D.", "Martin" },
			{ "Richard", "A.", "Tattersall" },
			{ "Philip", "B.", "Edwards" },
			{ "Moore", "T.", "Moore" },

			// shorten scrollbar grip with these ...

			{ "Lynn", "M.", "Seckinger" },
			{ "Carol", "R.", "Seckinger" },
			{ "Roy", "D.", "Martin" },
			{ "Richard", "A.", "Tattersall" },
			{ "Philip", "B.", "Edwards" },
			{ "Moore", "T.", "Moore" },
		},
		new Object[] {"First Name", "MI", longTitle});

	public Test() {
		TableColumn middleColumn = table.getColumn("MI");
		TableColumn lastColumn = table.getColumn(longTitle);

		lastColumn.setHeaderRenderer(multilineRenderer);
		table.getTableHeader().setToolTipText("Table Header!");
		getContentPane().add(
				     new JScrollPane(table),
BorderLayout.CENTER);
	}
	public static void main(String args[]) {
		GJApp.launch(new Test(),
			"Multi-Line Column Headers",300,300,300,250);
	}
}
class MultilineHeaderRenderer implements TableCellRenderer {
	MultilineHeader mll;
	JScrollPane scrollPane;

	public MultilineHeaderRenderer(String title) {
		mll = new MultilineHeader(title);
		scrollPane = new JScrollPane(mll);

		scrollPane.setHorizontalScrollBarPolicy(
			JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		scrollPane.setVerticalScrollBarPolicy(
			JScrollPane.VERTICAL_SCROLLBAR_NEVER);

		scrollPane.setBorder(null);
	}
	public Component getTableCellRendererComponent(JTable table,
								Object value,
								boolean
isSelected,
								boolean
hasFocus,
								int row, int
col) {
		mll.setText((String)value);
		return scrollPane;
	}
}
class MultilineHeader extends JTextArea {
	public MultilineHeader(String s) {
		super(s);
	}
	public void updateUI() {
		super.updateUI();

		// turn on wrapping and disable editing and highlighting
    Font oldFont = getFont();
    setFont(new Font(oldFont.getName(), oldFont.getStyle()|Font.BOLD,
oldFont.getSize()));
    //setFont(oldFont.deriveFont(oldFont.getStyle()|Font.BOLD));
		setLineWrap(true);
		setWrapStyleWord(true);
		setHighlighter(null);
		setEditable(false);

		// make the text area look like a table header

		LookAndFeel.installColorsAndFont(this,
										
"TableHeader.background",
										
"TableHeader.foreground",
										
"TableHeader.font");

		LookAndFeel.installBorder(this, "TableHeader.cellBorder");
	}
}
class GJApp extends WindowAdapter {
	static private JPanel statusArea = new JPanel();
	static private JLabel status = new JLabel(" ");
	static private ResourceBundle resources;

	public static void launch(final JFrame f, String title,
							  final int x, final int
y,
							  final int w, int h) {
		launch(f,title,x,y,w,h,null);
	}
	public static void launch(final JFrame f, String title,
							  final int x, final int
y,
							  final int w, int h,
							  String
propertiesFilename) {
		f.setTitle(title);
		f.setBounds(x,y,w,h);
		f.setVisible(true);

		statusArea.setBorder(BorderFactory.createEtchedBorder());
		statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		statusArea.add(status);
		status.setHorizontalAlignment(JLabel.LEFT);

		f.setDefaultCloseOperation(
							
WindowConstants.DISPOSE_ON_CLOSE);

		if(propertiesFilename != null) {
			resources = ResourceBundle.getBundle(
						propertiesFilename,
Locale.getDefault());
		}

		f.addWindowListener(new WindowAdapter() {
			public void windowClosed(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	static public JPanel getStatusArea() {
		return statusArea;
	}
	static public void showStatus(String s) {
		status.setText(s);
	}
	static Object getResource(String key) {
		if(resources != null) {
			return resources.getString(key);
		}
		return null;
	}
}
(Review ID: 104986) 
======================================================================

Comments
EVALUATION As far as we can tell this example works according to spec., the table makes the header high enough for the two rows specified in the header text of the last column (which contains a single \n in the example). Whether or not the header should change height when it is not wide enough to display the text on a single row is unspecified. The algorithms that are used in 1.3 are certainly much faster than those of 1.2.2 for normal tables and it may well be that some of that performance win comes exactly from not doing the calculations on which this example is contingent. If you consider that the code in Mr. Geary's book is now broken, I'm sure he'd appreciate it if you could let him know. If you home in on a specific failure/issue in the swing code base please file a bug which would allow us to figure out which component is at fault and how its behavior should be changed. Unless we hear more about this, we will assume that performance remains the highest of our priorities and that this change should not be reversed if (as we believe) perforamnce *may* be negatively impacted for 'normal' tables. philip.milne@eng 2000-07-13
13-07-2000