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: 6503981
Votes 77
Synopsis Bug in BasicTableHeaderUI
Category java:classes_swing
Reported Against
Release Fixed 7(b20), 6u4(b02) (Bug ID:2150882)
State 10-Fix Delivered, Verified, bug
Priority: 2-High
Related Bugs 6388189 , 6512107 , 6538743
Submit Date 13-DEC-2006
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 :
 customer  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.
Posted Date : 2007-06-29 22:32:18.0
Work Around
N/A
Evaluation
DELETED
Posted Date : 2007-06-29 20:27:35.0

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.
Posted Date : 2007-06-29 20:33:59.0

this bug is a regression introduced by the fix for 6388189 [JTable Header focus moves out with shifting of columns]
Posted Date : 2007-07-10 18:32:06.0
Comments
  
  Include a link with my name & email   

Submitted On 04-APR-2007
Christiaan_se
Any chance this is being fixed in the next update? We have several tables with many columns and since we like to be user friendly we have enabled D&D of the columns. This bug actually is the first thing our users notice! It also seems like a small fix to me.


Submitted On 17-APR-2007
fido7
Very frustrating.  This may kill a project I'm working on, as the user is almost required to rearrange columns in a large table.  Any workaround would be useful.  


Submitted On 27-APR-2007
forcers
FYI: even a simple scroll via chevron click to left causes the display to jump and prohibits correct scrolling.


Submitted On 16-MAY-2007
pleonard@lehman.com
we can't upgrade to v6 because of this - any idea when a fix might happen?


Submitted On 16-MAY-2007
pleonard@lehman.com
this prevents is from updating to v6, an idea when a fix might happen?


Submitted On 29-MAY-2007
sevenM
<quote>
we can't upgrade to v6 because of this - any idea when a fix might happen?
</quote>

Same here. Same question.


Submitted On 15-JUN-2007
A serious user interface problem


Submitted On 15-JUN-2007
A serious user interface problem


Submitted On 15-JUN-2007
A serious user interface problem


Submitted On 03-JUL-2007
Kleopatra
There are several issues with the code:

- the "selectedColumn" of the header is weirdly initialized and not really kept in synch (? should it) with "selected column" in the columnModel. 

- no need (AFAICS) to do the back/forth index conversion: all participants (view, ui-delegate, columnModel, mousevents) are in view coordinates 

No way to hook into the selected of the header except through a dirty hack: add a mouselistener to the header, force the current column to be selected and perform the action "focusHeader" (from the table's actionMap). Something like:

[code]
public void mousePressed(MouseEvent e) {
        int column = header.getColumnModel().getColumnIndexAtX(e.getPoint().x);
        if (column >= 0) {
            table.setColumnSelectionInterval(column, column);
            focusAction.actionPerformed(new ActionEvent(table, 0, "focusHeader"));
       }

}

[/code]

Jeanette


Submitted On 04-JUL-2007
mhoesch
Causes us to probably step back from migrating to Java 6 for a large swing application. 
Very serious Swing Bug!


Submitted On 19-JUL-2007
Very serious table bug that renders column movement with the mouse almost unusable.


Submitted On 14-AUG-2007
We've already moved forward with a release based on Java6, and its too late to move back to Java5.... a fix for this would be nice, this is a HUGE issue!


Submitted On 14-AUG-2007
Verified that  Kleopatra's hack (Submitted On 03-JUL-2007) works for us.


Submitted On 13-SEP-2007
I downloaded and tried Java Plug-in 1.6.0_02, but the problem still exists.  Did I download the wrong version?


Submitted On 14-SEP-2007
This bug has been fixed, but these versions, have not yet been released. If you look at the top of the report it says which versions this bug has been fixed:
Release Fixed  	  7(b20), 6u4(b02)

This means that it was fixed in 
   JDK7 build 20  (which you will find here (https://jdk7.dev.java.net/)
   JDK6 update 4, build 2  

-Roger


Submitted On 19-NOV-2007
I implemented Kleopatra's fix and had to ask her how the focusAction variable is obtained.  She answered and wanted me to update this bug with her answer.

The complete hack is as follows:

[code]
public void mousePressed(MouseEvent e) {
        int column = header.getColumnModel().getColumnIndexAtX(e.getPoint().x);
        if (column >= 0) {
            table.setColumnSelectionInterval(column, column);
            final Action focusAction = table.getActionMap().get("focusHeader");
            focusAction.actionPerformed(new ActionEvent(table, 0, "focusHeader"));
       }
}
[/code]


Submitted On 01-FEB-2008
Knarf76
The trick of Kleopatra works, and the new Java6u4 also works. But both have the same small problem.

When you drag a column that is in the middle of the JScrollPane and you want to drag it all the way to the right or left you have to:
- Move the column
- Slide the JScrollPane a little
- Move the column
- Slide the JScrollPane a little
- ...
- Until you reached the correct position

With Java6u4 sometimes the JScrollPane does autoscrolling (I think with small (width) columns) but mostly not.

Is there a way so that when you reach the edge of the JScrollPane while dragging a column that the JScrollPane starts scrolling automicly ?


Submitted On 14-AUG-2008
bhurley
In reply to Knarf76:

this is what I have found.  Subclass JTable and add this code: 
[code] 
private MouseMotionListener mouseMotionDraggedAdaptor = new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent e) {
          Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1);
          scrollRectToVisible(r);
    }
};

getTableHeader().setAutoscrolls(true);  

getTableHeader().addMouseMotionListener(mouseMotionDraggedAdaptor);  
[/code}


Submitted On 09-MAR-2009
Georgy_Dimitrov
Hi , I apply this solution.But I will rewrite UI later.

JTable  _table = new JTable() {
  public int convertColumnIndexToView(int modelColumnIndex) {
    if (getTableHeader().getDraggedColumn() != null && getAutoResizeMode() == JTable.AUTO_RESIZE_OFF) {
      return super.convertColumnIndexToView(getTableHeader().getDraggedColumn().getModelIndex());
    }
    return super.convertColumnIndexToView(modelColumnIndex);
  }

};

getTableHeader().getDraggedColumn()  != null only we drag column



PLEASE NOTE: JDK6 is formerly known as Project Mustang