Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4310721
Votes 4
Synopsis JTable is not stretched to fill a viewport's hieght
Category java:classes_swing
Reported Against 1.3 , kestrel-rc1
Release Fixed mustang(b53)
State 10-Fix Delivered, bug
Priority: 4-Low
Related Bugs 4464210
Submit Date 08-FEB-2000
Description


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) 
======================================================================
Posted Date : 2005-08-26 19:45:11.0
Work Around




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   xxxxx@xxxxx  
 */
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);
         }
   }
}
======================================================================
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.
  xxxxx@xxxxx   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.
  xxxxx@xxxxx   2001-10-09
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)
Posted Date : 2005-08-31 14:59:26.0
Comments
  
  Include a link with my name & email   

Submitted On 29-SEP-2000
jasoncc
I'd like to see the JTable stretched to fill the viewport.


Submitted On 30-JAN-2001
kuhse
It's not just about the background color.
Sometimes it's desirable that the user
can open a popup menu by clicking on an
empty area of a table (see Windows Explorer,
for example). However, for the appropriate
mouse listener to work, the table needs to
BE THERE in the first place. A viewport
background pretending to be part of the
table won't do.


Submitted On 05-AUG-2004
scottvw
On newer JVM's (1.4, 1.5) the BugFixedViewportLayout workaround seems to interfere with the drawing of the applet. I had a very simple table who's TableModel refreshed every 5 seconds and the Applet slowed to a crawl. If I scrolled the page in my web browser, it wouldn't redraw correctly, either.


Submitted On 03-OCT-2008
ksorr
As scottvw comments, the supplied code above causes excessive repaints when the table's preferred size is smaller than the viewport size. When I comment out the call to super.layoutContainer(), everything seems to calm down. I'm not sure what the parent class (ViewportLayout is doing that is causing this, but omitting it seems to be OK).

Note I'm using Java 1.5.0_13 on the Mac.



PLEASE NOTE: JDK6 is formerly known as Project Mustang