File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Notifying listeners of changes in jtable cell selection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Notifying listeners of changes in jtable cell selection" Watch "Notifying listeners of changes in jtable cell selection" New topic
Author

Notifying listeners of changes in jtable cell selection

Tom Hughes
Ranch Hand

Joined: Feb 09, 2002
Posts: 86
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

Joined: Feb 09, 2002
Posts: 86
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));
}
}
}
}


}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Notifying listeners of changes in jtable cell selection
 
Similar Threads
unable to insert JRadio in JTable.
JTable
Prevent selection of certain JTable cells
Tabbing between cells in a JTable
Tabbing between editable cells in JTable