United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6503981 Bug in BasicTableHeaderUI
JDK-6503981 : Bug in BasicTableHeaderUI

Details
Type:
Bug
Submit Date:
2006-12-13
Status:
Closed
Updated Date:
2011-03-07
Project Name:
JDK
Resolved Date:
2011-03-07
Component:
client-libs
OS:
windows_xp,windows_2000
Sub-Component:
javax.swing
CPU:
x86
Priority:
P2
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Backport:
Duplicate:
Duplicate:
Relates:

Sub Tasks

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
SUGGESTED FIX

DELETED
                                     
2007-01-16
EVALUATION

DELETED
                                     
2007-01-16
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.
                                     
2007-06-29
EVALUATION

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



Hardware and Software, Engineered to Work Together