1
votes

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);
    }
}
1
(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
@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

1 Answers

2
votes

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.