Spinner and TableEvent

I have a table cell that uses a JSpinner. Everytime I change the value of that cell, either to increase or decrease its value, the TableEvent is never get fired.

It's fired ONLY when I click outside that cell.

Here is a snippet of my code :

publicclass SpinnerRendererextends JSpinnerimplements TableCellRenderer{

public SpinnerRenderer(){

}

public Component getTableCellRendererComponent(JTable table, Object value,

boolean isSelected,boolean hasFocus,int row,int column){

setValue(value);

fireStateChanged();

returnthis;

}

}

publicclass SpinnerEditorextends AbstractCellEditorimplements TableCellEditor{

protected JSpinner spinner =new JSpinner();

public SpinnerEditor(){

spinner.setModel(new SpinnerNumberModel(1, 0, 999, 1));

}

publicboolean stopCellEditing(){

if (!isCommitted()){

returnfalse;

}

return super.stopCellEditing();

}

protectedboolean isCommitted(){

try{

spinner.commitEdit();

}catch (Exception ex){

ex.printStackTrace();

}

returntrue;

}

public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected,int row,int column){

spinner.getModel().setValue(value);

return spinner;

}

publicboolean isCellEditable(EventObject evt){

returntrue;

}

public Object getCellEditorValue(){

Object obj = spinner.getValue();

return obj;

}

}

publicclass MyTableModelextends AbstractTableModel{

...

publicvoid initColumnModel(TableColumn pColumn,int pColIndex){

switch (pColIndex){

case 1:

pColumn.setCellEditor(new SpinnerEditor());

pColumn.setCellRenderer(new SpinnerRenderer());

break;

default:

return;

}

}

publicvoid setValueAt(Object pObject,int pRow,int pCol){

...

switch (pCol){

case 1:

mydata.setNumberOfPages(((Integer) pObject).intValue());

fireTableCellUpdated(pRow, pCol);

break;

default:

return;

}

}

...

}

publicclass MyPanelextends JPanel{

...

private MyTableModel model =new MyTableModel();

public MyPanelX(){

model.addTableModelListener(new TableModelListener(){

publicvoid tableChanged(TableModelEvent e){

log.debug("==> TableModelEvent : " + e.getSource());

if (e.getType() == TableModelEvent.UPDATE){

int row = e.getFirstRow();

int column = e.getColumn();

if (column == 1){

log.info("==> tableChanged on column 1 detected.");

}

}

}

});

}

...

How can I fire a TableEvent or any event every time I key a value or click on the up-arrow or down-arrow of the spinner ? What am I missing ?

Thanks for any help/suggestions.

[6807 byte] By [nusaa] at [2007-11-27 9:29:23]
# 1

the behavior is just perfect, because when you click outside of the cell then only final updation of the table model is done, so if you want to get the event on Spinner then you have to add listener to the spinner, i mean the cell editor. in your case SpinnerEditor.spinner

regards

Aniruddha

Aniruddha-Herea at 2007-7-12 22:37:39 > top of Java-index,Desktop,Core GUI APIs...
# 2

If you want to listen when value in spinner is changed when you click next or previous button of spinner then you can use ChangeListener class.

You create following class:

public class SpinnerStateChangeListener implements ChangeListener {

public void stateChanged(ChangeEvent e) {

int changedValue = Integer.parseInt(((SpinnerEditor) evt

.getSource()).getValue);

System.out.println("spinner value changed: changed value= "+changedValue)

}// end of stateChanged()

}// end of class SpinnerStateChangeListener

And use it like:

SpinnerEditor spinnerEditor = new SpinnerEditor();

TableColumn column = sometable.getColumnModel()

.getColumn(0);//column index where you have spinner

column.setCellEditor(spinnerEditor);

spinnerEditor.addChangeListener(new SpinnerStateChangeListener());

PawanPa at 2007-7-12 22:37:39 > top of Java-index,Desktop,Core GUI APIs...