aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable Header column listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Header column listener" Watch "JTable Header column listener" New topic
Author

JTable Header column listener

Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
Hi,

I want to add a listener to my custom header renderer, that runs if a column have changed position through draging (draging a column eighter left or right to change its position). I know that column indexes won't change even after doing that, but I still need that kind of listener. Right now I am using a MouseMotionListener, but it is not fast enough, and I only want to use it as a last solution.

Is there any listener that I can use like I explained?

Thanks is advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

TableColumnModelListener has a columnMoved method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
Rob Prime wrote:TableColumnModelListener has a columnMoved method.


That sounds like a good idea, but is there noway to get access to that funktionality inside my custom table header, since that is what I want, if possible?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

Why wouldn't there be a way to use it? Clearly the class wasn't written for nothing.
Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
Paul Clapham wrote:Why wouldn't there be a way to use it? Clearly the class wasn't written for nothing.


I know that, but TableColumnModelListener is made to be used as a listener for TableColumnModel, right? But I would like to use it with my custom table header that extends DefaultTableCellRenderer, and theres no method there to add this listener, so I was wondering if theres some way to get around it. Since I would like that funktionality from my header
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Alexander Walker wrote:I know that, but TableColumnModelListener is made to be used as a listener for TableColumnModel, right?

Right.

But I would like to use it with my custom table header that extends DefaultTableCellRenderer,

And that's where you're wrong. Your table header is an instance of JTableHeader. You're using a custom renderer to render its column headers, but the header itself is still a JTableHeader. And that means you have access to its JTable, and to its TableColumnModel, and that's where you add the listener to.

You see, there are multiple classes that work very closely together. Every JTable has a TableColumnModel and a JTableHeader. The JTable itself only shows the contents. The JTableHeader shows the header. The TableColumnModel determines for both of them which columns to show and in which order. If you drag around a column the renderers have nothing to do with that - except show the contents of the header / cells.
Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
Rob Prime wrote:Your table header is an instance of JTableHeader. You're using a custom renderer to render its column headers, but the header itself is still a JTableHeader. And that means you have access to its JTable, and to its TableColumnModel, and that's where you add the listener to.

You see, there are multiple classes that work very closely together. Every JTable has a TableColumnModel and a JTableHeader. The JTable itself only shows the contents. The JTableHeader shows the header. The TableColumnModel determines for both of them which columns to show and in which order. If you drag around a column the renderers have nothing to do with that - except show the contents of the header / cells.


I understand what you mean now. You explained it in a good way. Thank you for answering my question.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable Header column listener
 
Similar Threads
JTable with multicolumn header
Drag Images from One Table Column to Other
What listener for JTable header column size change
Nested JTables - The header of the inner JTable is not showing
JTable