• Post Reply Bookmark Topic Watch Topic
  • New Topic

SelectionChangedListener?

 
Meyer Florian
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple question... hehe
Is there something like a SelectionListener or SelectionChangedListener for JTable? Hope u know what I mean...
I really didn't find anything...
Thanks and greets from Praiser
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Meyer.
This is from JTable API

getSelectionModel
public ListSelectionModel getSelectionModel()
Returns the ListSelectionModel that is used to maintain row selection state.
Returns:
the object that provides row selection state, null if row selection is not allowed

There are also others methods:

int getSelectedColumn()
Returns the index of the first selected column, -1 if no column is selected.
int getSelectedColumnCount()
Returns the number of selected columns.
int[] getSelectedColumns()
Returns the indices of all selected columns.
int getSelectedRow()
Returns the index of the first selected row, -1 if no row is selected.
int getSelectedRowCount()
Returns the number of selected rows.
int[] getSelectedRows()
Returns the indices of all selected rows.

and from javax.swing.table.DefaultTableColumnModel

int getSelectedColumnCount()
Returns the number of columns selected.
int[] getSelectedColumns()
Returns an array of selected columns.
ListSelectionModel getSelectionModel()
Returns the ListSelectionModel that is used to maintain column selection state.
 
Meyer Florian
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for info!
But I can't find any Listener...
My only solution is to add a MouseListener with mousePressed() and a KeyListener with keyPressed() on my JTable. That KeyListener catches all keys that could change the Cell selection.
But then: What if I programmatically change the selection?
So there is really no listener availible in Java for that event... :-|
Praiser
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, what listener dou you want?
JTable itself implements ListSelectionListener to be notified when rows selection changes. You can retrieve the selected row(s) with methods shown above.
Similarly, DefaultTableColumnModel implements the same interface to be notified of changes in the column selection.
If you use setRowSelectionInterval to programatically set the selected rows, the ListeSelectionEvents are automatically generated by it!
[ December 09, 2003: Message edited by: Jose Botella ]
 
Meyer Florian
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WOW!!!
That smashed like a bomb into me
Thank you so much, it works...
Didn't see that JTable implements a Listener... thought something like addListSelectionListener(ListSelectionListener l); should exist as a method in JTable...
And another question:
When I change the Selection via the keyboard (down-key etc...), the event valueChanged() of the ListSelectionListener gets invoked. When I select a row with my mouse, the valueChanged() event gets invoked twice: once for mousePressed() and once for mouseReleased().
Do you know how to set the mouseReleased() method to do nothing?
When I add a MouseListener and implement the mouseReleased() method and leave it empty, the valueChanged() event gets invoked twice anyway...
Thanks again!
Praiser
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this code will clear your doubts.

It only prints one event when the selection gesture ends.
 
Meyer Florian
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that's it!
e.getValueIsAdjusting();
Returns true if this is one of multiple change events.
Thanks so much :-)
Praiser
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!