JDK-6503981 : Bug in BasicTableHeaderUI
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2006-12-13
  • Updated: 2011-03-07
  • Resolved: 2011-03-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7
6u4Fixed 7 b20Fixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0-rc"
Java(TM) SE Runtime Environment (build 1.6.0-rc-b99)
Java HotSpot(TM) Client VM (build 1.6.0-rc-b99, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP
Professional
Version 2002
Service Pack 2

A DESCRIPTION OF THE PROBLEM :

If you run the code below you will get a table, scroll right to the last table select column 39 and start moving it to the left. The column will jump straight to the beginning of the table instead of a normal move. The move triggers scrollRectToVisible which outputs:
scroll to: left=0 right=75 visible: left=2511.0 right=3000.0
scroll to: left=0 right=75 visible: left=0.0 right=489.0
scroll to: left=0 right=75 visible: left=0.0 right=489.0
scroll to: left=0 right=75 visible: left=0.0 right=489.0

I think this bug is caused in BasicTableHeaderUI.mouseDragged(MouseEvent e)
....
//Cache the selected column.
int selectedIndex = table.convertColumnIndexToModel(getSelectedColumnIndex());

//Now do the move.
cm.moveColumn(columnIndex, newColumnIndex);

//Update the selected index.
selectColumn(table.convertColumnIndexToView(selectedIndex));
return;
...

selectedIndex is 0 and will be 0 after setting the new selected column. I think the call should be selectColumn(newColumnIndex), although the caching of the selectedIndex is probably done for a reason?

Testcase:
-- TestCase.java
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TestCase extends JFrame {    
    public TestCase() {    	
        super("test");    	
        setPreferredSize(new Dimension(500, 500));    	
        createContent();
    }        
    public void createContent() {
    	setLayout(new BorderLayout());    	
        JTable table = new JTable() {    	
                @Override
                public void scrollRectToVisible(Rectangle aRect) {   
                    double leftVisible = getVisibleRect().getX(); 
                    double rightVisible = getVisibleRect().getX() + getVisibleRect().getWidth();
                    System.out.println("scroll to: left=" + aRect.x 
                                       + " right=" + (aRect.x + aRect.width) 
                                       + " visible: left=" + leftVisible 
                                       + " right=" + rightVisible);   
                    super.scrollRectToVisible(aRect); 
   		}    	
            };
        table.setModel(new MyTableModel());
    	table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);    	
        JScrollPane sp = new JScrollPane(table);
    	this.add(BorderLayout.CENTER, sp);    	    
    }
    public class MyTableModel extends DefaultTableModel {
        @Override		
        public int getColumnCount() {	
            return 40;		
        }		
        @Override		
        public String getColumnName(int column) {
            return "column " + column;
        }
        @Override
        public int getRowCount() {
            return 10;	
	}	
	@Override
        public Object getValueAt(int row, int column) {	
            return "" + row + ", " + column;	
	}
    }	
    /**	 @param args	 */	
    public static void main(String[] args) {
        TestCase tc = new TestCase();	
	tc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tc.pack();
        tc.setVisible(true);
    }
}
--
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
see above

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No "jumping" when scrolling
ACTUAL -
see above

REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
None, I can't override relevant methods

Release Regression From : 5.0u6
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
EVALUATION this bug is a regression introduced by the fix for 6388189 [JTable Header focus moves out with shifting of columns]
10-07-2007

EVALUATION In BasicTableHeaderUI.MouseInputHandler.mouseDragged, there is the following: //Update the selected index. selectColumn(table.convertColumnIndexToView(selectedIndex)); I beleive this is *almost* right. We do want to keep the selected column updated. The problem is that selectColumn also scrolls to the selected column. While this makes sense for keyboard updates to the selected column, it does not make sense when we're using it like this. As such, I think we may need to parameterize whether or not the scrolling happens.
29-06-2007

SUGGESTED FIX DELETED
16-01-2007

EVALUATION DELETED
16-01-2007