1

Firstly I apologize if I can not express to 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 table 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 table 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 table 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);
    }
}
Iqbal Hossain
  • 97
  • 3
  • 5
  • 16
  • (not attack) do you understant Renderers and Editors Concept for JTable (event JTree, JComboBox), if is there real MyDialog dg = new MyDialog(null,true);, then you can to pass value directly to XxxTableModel, then Editor is quite useless – mKorbel Aug 12 '13 at 20:33
  • @mKorbel: Thanks for your comment. And probably I am going to follow your suggestion and skip the above mentioned procedure to get the editor value for Column 0. For column selection I can get value from dialog and easily setValueAt to TableModel. Which is more easy. – Iqbal Hossain Aug 13 '13 at 04:05

1 Answers1

2

How can I hold the object from TableCellEditor class and pass it to setValueAt() when JTable automatically trigger to put the cell value to table model?

As discussed here, you should not: "The table's editingStopped() method collects the new value via getCellEditorValue() and uses it to setValueAt() in the model." There's no need for your TableCellEditor to fireEditingStopped(). As an aid to understanding, I found it helpful to break on editingStopped() in a debugger to examine the call stack.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for your answer. In fact I found my editingStopped() method was not called by JTable when fireEditingStopped() Method and found fireEditingStopped is useless. Although I could not solve the problem but try to find any solution. – Iqbal Hossain Aug 13 '13 at 04:02
  • You might edit your question to clarify the goal, rather than just showing what doesn't work; an [sscce](http://sscce.org/) that shows you you apply the `TableCellEditor` would be easier to examine. – trashgod Aug 13 '13 at 14:32