Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable Header column listener

 
Alexander Walker
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TableColumnModelListener has a columnMoved method.
 
Alexander Walker
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why wouldn't there be a way to use it? Clearly the class wasn't written for nothing.
 
Alexander Walker
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic