0

i have a Java program which has a table and a table model. Each cell is a JTextField with a mouselistener on it.

public class ButtonCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener, MouseListener{  

private JTextField lab;

public ButtonCellEditor(MonthManager mm) {
    super();
    lab=new JTextField();
    lab.addMouseListener(this);     
}

when i click the cell it opens another JDialog to edit cell background information which calculates sum of different values

@Override
public void mouseClicked(MouseEvent arg0) {
    CellDialog bd = new CellDialog(mm,mm.getMonths().get(column));  
    bd.openDialog();
    //lab.setText("34");                
}

In this Dialog I have some Components to calculate Data. One is an apply button which shall update the cell information that was clicked (and sometimes also other cells) If this apply buton is clicked an actionlistener is called which updates at least the cell i have clicked on via the table model

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() instanceof JButton){
        model.setValueAt(cellSum, i, columnIndex);  // <-- Cell where I clicked on
        model.setValueAt(anotherSum, i+1, columnIndex);  // <-- another cell
    }
}

What happens now is that the other cell gets updated but the cell where i clicked on not (after closing the dialog). The code is only small snippets showing the basic thing. The overall code would be too much here and only irritating since im quite sure that it has to do with the cell event. Somehow the table content cannot be changed while it is focused or as long as it is handling the mouselistener event. If I enable he row above "lab.setText("34");" this gets writen into the cell when i close the dialog, but never the value i wrote into the table model.

Setting the Dialog no non-modal doesnt change this. Only advantage is that the other cells get updated while the dialog is still open, but not the clicked cell. Best case would be if I could update all cells as non-modal to see the changes live.

Hope I explained my issue well and all needed information is there.

Please let me know if you have an idea since after several hours trying different things and searchig in the net I couldnt solve this yet.

Thanks a lot!

Tobi

Tobi
  • 1
  • 1

2 Answers2

0

the other cell gets updated but the cell where i clicked on not

This is because the editor will update the TableModel when the editor is stopped. So it will overwrite your code in the dialog.

In general an editor should only update a single cell.

If this value causes changes to other cells then you should either:

  1. Override the setValueAt(...) method of your TableModel to update the other value in the other cell
  2. Use a TableModelListener to be notified of the change in value. Check out: https://stackoverflow.com/a/3541876/131872 for a working example of this approach.
camickr
  • 321,443
  • 19
  • 166
  • 288
  • hello camickr, thanks for your answer. Im not sure if i understand you correctly. What do you mean by the editor will update the tablemodel when the editor is stopped? Mainly its about the cell which i clicked on. This one doesnt get updated at all as long as it is "focused" by the click – Tobi Oct 26 '20 at 20:08
  • Normally you edit a cell by double clicking and then a JTextField is display as the editor. You type your text into the editor. You then use your mouse or click on another cell at which time cell editing is stopped and the data you type into the text if copied to the TableModel. While the cell is in editing mode the editor is active, so the text has not yet been copied to the TableModel. – camickr Oct 27 '20 at 01:06
0

finally I found another thread that solved it. Your answer camikr in this thread helped me:

Is there an elegant way to make JTable stop editing when user closes the application window?

So i simply added

dataTab.getCellEditor().stopCellEditing();

in the actionlistener of the dialogs button. Then the edit mode was removed and the cell was updated.

Thanks! Tobi

Tobi
  • 1
  • 1