英文:
Updating java table model while having dialog opened triggered by cell
问题
我有一个Java程序,其中包含一个表格和一个表格模型。每个单元格都是一个带有鼠标监听器的JTextField。
public class ButtonCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener, MouseListener {
private JTextField lab;
public ButtonCellEditor(MonthManager mm) {
super();
lab = new JTextField();
lab.addMouseListener(this);
}
当我点击单元格时,它会打开另一个JDialog,用于编辑单元格背景信息,该信息会计算不同值的总和。
@Override
public void mouseClicked(MouseEvent arg0) {
CellDialog bd = new CellDialog(mm, mm.getMonths().get(column));
bd.openDialog();
// lab.setText("34");
}
在这个对话框中,我有一些组件来计算数据。其中一个是一个“应用”按钮,它将更新被点击单元格的信息(有时还会更新其他单元格)。
如果点击了这个“应用”按钮,将调用一个动作监听器,通过表格模型来更新至少我点击的单元格。
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
model.setValueAt(cellSum, i, columnIndex); // < -- 我点击的单元格
model.setValueAt(anotherSum, i + 1, columnIndex); // < -- 另一个单元格
}
}
现在发生的情况是,另一个单元格被更新了,但是我点击的单元格没有被更新(在关闭对话框后)。这段代码只是展示了基本内容。完整的代码在这里会太多,而且只会让人困惑,因为我很确定问题与单元格事件有关。不知何故,在处理鼠标监听器事件时,似乎无法更改表格内容。只有在它失去焦点或者正在处理鼠标监听器事件时,才能进行更改。
如果我取消注释上面的一行“lab.setText("34");”,在关闭对话框时,这将写入单元格,但是我写入表格模型的值从未出现。
将对话框设置为非模态并不能改变这种情况。唯一的好处是在对话框仍然打开时,其他单元格会被更新,但被点击的单元格不会被更新。最好的情况是,我可以将所有单元格都设置为非模态,以实时看到更改。
希望我能清楚地解释我的问题,并提供了所有必要的信息。
如果你有什么想法,请告诉我,因为在尝试了几个小时不同的方法并在网络上搜索后,我仍然没有解决这个问题。
非常感谢!
Tobi
英文:
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
答案1
得分: 0
> 另一个单元格会被更新,但我点击的单元格不会被更新。
这是因为当编辑器停止时,编辑器会更新TableModel。因此,它会覆盖对话框中的您的代码。
一般来说,编辑器只应更新单个单元格。
如果此值导致其他单元格发生更改,那么您应该执行以下操作之一:
- 重写您的TableModel的
setValueAt(...)
方法,以更新另一个单元格中的其他值。 - 使用TableModelListener来接收有关值更改的通知。请参阅:https://stackoverflow.com/a/3541876/131872,了解此方法的工作示例。
英文:
> 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:
- Override the
setValueAt(...)
method of your TableModel to update the other value in the other cell - 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.
答案2
得分: 0
终于我找到了另一个解决方法的线程。你在这个帖子中的回答帮助了我:
所以我简单地添加了
dataTab.getCellEditor().stopCellEditing();
在对话框按钮的动作监听器中。然后编辑模式被移除了,单元格得以更新。
谢谢!
Tobi
英文:
finally I found another thread that solved it. Your answer camikr in this thread helped me:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论