United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4310721 : JTable is not stretched to fill a viewport's hieght

Details
Type:
Bug
Submit Date:
2000-02-08
Status:
Resolved
Updated Date:
2006-01-19
Project Name:
JDK
Resolved Date:
2005-09-20
Component:
client-libs
OS:
windows_nt,generic
Sub-Component:
javax.swing
CPU:
x86,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.3.0
Fixed Versions:

Related Reports
Relates:

Sub Tasks

Description
Name: krT82822			Date: 02/07/2000


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

  Bug was introduced in Swing 1.1, JDK 1.2

The bug manifests itself like this:
The background color is not painted correctly when JTable is in a
JScollPane and the size of the table is smaller than the scrollpane.

The bug is that ViewportLayout in Swing1.1, JDK1.2 and JDK1.3 does not
stretch the component correctly.  This used to work in Swing 1.0.3.

This attached code illustrates the problem and has a fix.
Related bugs: 4237176, 4119459, 4156773, 4189341

This bug does not effect JList, only JTable, because of this difference
in the implementation of Scrollable:  (this is another workaround)

JList.java:
    /**
     * Returns true if this <code>JList</code> is displayed in a
     * <code>JViewport</code> and the viewport is taller than
     * <code>JList</code>'s preferred height; otherwise returns false.
     * If false, then don't track the viewport's height. This allows vertical
     * scrolling if the <code>JViewport</code> is itself embedded in a
     * <code>JScrollPane</code>.
     *
     * @return true if viewport is taller than <code>Jlist</code>'s
     *				preferred height, otherwise false
     * @see Scrollable#getScrollableTracksViewportHeight
     */
    public boolean getScrollableTracksViewportHeight() {
	if (getParent() instanceof JViewport) {
	    return (((JViewport)getParent()).getHeight() > getPreferredSize
().height);
	}
	return false;
    }



    /**
     * Returns false to indicate that the height of the viewport does not
     * determine the height of the table.
     *
     * @return false
     * @see Scrollable#getScrollableTracksViewportHeight
     */
    public boolean getScrollableTracksViewportHeight() {
        return false;
    }
(Review ID: 100892) 
======================================================================

                                    

Comments
EVALUATION

Phil, you are going to have to make the call as to what should happen here.
If you want JTables height to be at least the size of the viewport than change getScrollableTracksViewportHeight to match that of JList or JTree. On the other hand, if you don't think the height of JTable should be at least that of the JViewport, than close this bug out. To get the background color to work developers can always set the background color on the JViewport.
scott.violet@eng 2000-02-08

Another reason to make this change is because of dnd. If the table is smaller than the viewport dnd will only work over the table portion, when most users expect it to work over the complete region.
###@###.### 2001-10-09
                                     
2001-10-09
WORK AROUND



Name: krT82822			Date: 02/07/2000


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

public class ViewportLayoutBug
{
	public static void main(String[] args)
	{
        // this table does not stretch to the bottom
        JTable badTable = new JTable(new NumericTableModel());
        badTable.setPreferredScrollableViewportSize(new Dimension(100,100));
        
        // this list does stretch to the bottom
        JList list = new JList(new Object[]{ "1", "2", "3" });
        list.setPreferredSize(new Dimension(100,100));
        
        // this table does stretch to the bottom
        JTable goodTable = new JTable(new NumericTableModel());
        goodTable.setPreferredScrollableViewportSize(new Dimension(100,100));
        JScrollPane goodScrollPane = new JScrollPane(goodTable);
        goodScrollPane.getViewport().setLayout(new BugFixedViewportLayout());
        
        JPanel p = new JPanel(new BorderLayout());
        p.add(BorderLayout.WEST, new JScrollPane(badTable));
        p.add(BorderLayout.CENTER, new JScrollPane(list));
        p.add(BorderLayout.EAST, goodScrollPane);
        
        JFrame f = new JFrame();
        f.getContentPane().add(p);
		f.pack();
		f.show();
	}
}

class NumericTableModel extends AbstractTableModel
{
    public int getColumnCount() { return 1; };
    
    public int getRowCount() { return 3; };
    
    public Object getValueAt(int row, int col)
    {
        return new Integer((1+row)*(1+col));
    }
}

/**
 * A modified ViewportLayout to fix the JFC bug where components
 * that implement Scrollable do not resize correctly, if their
 * size is less than the viewport size.
 *
 * This is a JDK1.2.2 bug. This used to work in Swing 1.0.3 and
 * the fix is putting the old logic back.
 *
 * @author ###@###.###
 */
class BugFixedViewportLayout extends ViewportLayout
{
     public void layoutContainer(Container parent) {
         super.layoutContainer(parent);

         JViewport vp = (JViewport)parent;
         Component view = vp.getView();

         if(view == null) {
             return;
         }

         Point viewPosition = vp.getViewPosition();
         Dimension viewPrefSize = view.getPreferredSize();
         Dimension vpSize = vp.getSize();
         Dimension extentSize = vp.toViewCoordinates(vpSize);
         Dimension viewSize = new Dimension(viewPrefSize);

         if ((viewPosition.x == 0) && (vpSize.width > viewPrefSize.width)) {
             viewSize.width = vpSize.width;
         }
         
         if ((viewPosition.y == 0) && (vpSize.height > viewPrefSize.height)) {
             viewSize.height = vpSize.height;
         }

         if (!viewSize.equals(viewPrefSize)) {
             vp.setViewSize(viewSize);
         }
   }
}
======================================================================
                                     
2004-09-25
EVALUATION

Because we're not sure what developers want in their existing applications and we don't want to break anything, a new property has been added to control this behavior. To make JTable stretch to fill the viewport:

table.setFillsViewportHeight(true)
                                     
2005-08-31



Hardware and Software, Engineered to Work Together