Monday, 12 August 2013

How to pass object from table cell editor to Table Model?

How to pass object from table cell editor to Table Model?

Firstly I apologize if I can not make my problem in a convenient manner
because of my little knowledge of Java Swing.
I have a JTable with 4 columns. The JTable cell is editable and hold an
AbstractTableModel. I would like to show a dialog box with a list when the
first cell is going to edit mode (I have done this well). After choosing
the data from a list it returns an object (I can access the object). But
as a editor is JTextField it will display one sub value from the object (I
have done this also). But when the cell changes its position to another
cell JTable puts the value using setValueAt method to data model. But this
time it returns only the text which is hold by editor. In fact it should
return an Object to put the object in data model.
I can not understand how can I hold the object from TableCellEditor Class
and pass it to setValueAt when JTable automatically trigger to put the
cell value to data model? For better understanding I have mention my codes
below.
public class myobject {
public String id;
public String name;
public String tag;
}
public class My_Table_Cell_Editor extends AbstractCellEditor implements
TableCellEditor {
private myobject curr_val;
@Override
public Component getTableCellEditorComponent(JTable table, Object
value, boolean isSelected, int row, int column) {
Component c = table.getEditorComponent();
if(c==null){
return null;
}
if(column == 0){
MyDialog dg = new MyDialog(null,true);
dg.setVisible(true);
if("OK".equals(dg.button_state)){
((JTextField)c).setText(dg.return_record.record_name);
curr_val = dg.return_record; // return_record is myobject
fireEditingStopped();
}
}
((JTextField)c).selectAll();
return c;
}
@Override
public Object getCellEditorValue() {
return curr_val;
}
}
public class myTableModel extends AbstractTableModel{
Other codes are as usual...
@Override
public void setValueAt(Object value, int row, int col) {
rec_model rec = rec_arr.get(row);
switch (col) {
case 0:
rec.myobj = (myobject) value;
break;
}
rec_arr.set(row, rec);
fireTableCellUpdated(row, col);
}
}

No comments:

Post a Comment