This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Notifying listeners of changes in jtable cell selection

 
Tom Hughes
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a Class called GridEditor that extends JTable. One thing I want to do is to be able to add listeners to a GridEditor that get informed whenever the cell selection changes. I use the GridListener class below to act as a listener to changes. I use it like this in my constructer.
GridListener gridlistener = new GridListener(this);
getSelectionModel().addListSelectionListener(gridlistener);
colModel.addColumnModelListener(gridlistener);
The problem is when the user changes row and columns simultaneosly I end up firing two events (one for the change in row and one for the change in column). Does anyone know anyway round this ? All help gratefully recived,
Tom
//This class listens to changes in row and column selection.
//When these occur it fires a selectionChanged event to all registered GridEditorListeners
private class GridListener implements ListSelectionListener, TableColumnModelListener {

private GridEditor gridEditor;
private int row = 0;
private int col = 0;

public GridListener(GridEditor gridEditor) {
this.gridEditor = gridEditor;
}

public void columnAdded(TableColumnModelEvent e){}
public void columnRemoved(TableColumnModelEvent e){}
public void columnMoved(TableColumnModelEvent e){}
public void columnMarginChanged(ChangeEvent e){}
public void columnSelectionChanged(ListSelectionEvent e){
if (!e.getValueIsAdjusting()) {
doUpdate(row, gridEditor.getSelectedColumn());
}
}

public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
doUpdate(gridEditor.getSelectedRow(), col);
}
}

private void doUpdate(int newRow, int newCol) {
if (newCol != col || newRow != row) {
col = newCol;
row = newRow;

// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==GridEditorListener.class) {
((GridEditorListener)listeners[i+1]).selectionChanged(row, col, gridEditor.getValueAt(row, col));
}
}
}
}


}
 
Tom Hughes
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ignore me! I fixed my problem about a minute after posting !
If anyone is interested, my new GridListener looks like this,
private class GridListener implements ListSelectionListener, TableColumnModelListener {

private GridEditor gridEditor;
private int row = 0;
private int col = 0;

public GridListener(GridEditor gridEditor) {
this.gridEditor = gridEditor;
}

public void columnAdded(TableColumnModelEvent e){}
public void columnRemoved(TableColumnModelEvent e){}
public void columnMoved(TableColumnModelEvent e){}
public void columnMarginChanged(ChangeEvent e){}
public void columnSelectionChanged(ListSelectionEvent e){
if (!e.getValueIsAdjusting()) {
doUpdate();
}
}

public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
doUpdate();
}
}

private void doUpdate() {
int newRow = gridEditor.getSelectedRow();
int newCol = gridEditor.getSelectedColumn();

if (newCol != col || newRow != row) {
col = newCol;
row = newRow;

// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==GridEditorListener.class) {
((GridEditorListener)listeners[i+1]).selectionChanged(row, col, gridEditor.getValueAt(row, col));
}
}
}
}


}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic