更新 Java 表格模型时,同时打开由单元格触发的对话框。

huangapple go评论81阅读模式
英文:

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(&quot;34&quot;);				
}

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);  // &lt;-- Cell where I clicked on
model.setValueAt(anotherSum, i+1, columnIndex);  // &lt;-- 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。因此,它会覆盖对话框中的您的代码。

一般来说,编辑器只应更新单个单元格。

如果此值导致其他单元格发生更改,那么您应该执行以下操作之一:

  1. 重写您的TableModel的setValueAt(...)方法,以更新另一个单元格中的其他值。
  2. 使用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:

  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.

答案2

得分: 0

终于我找到了另一个解决方法的线程。你在这个帖子中的回答帮助了我:

https://stackoverflow.com/questions/25771338/is-there-an-elegant-way-to-make-jtable-stop-editing-when-user-closes-the-applica

所以我简单地添加了

dataTab.getCellEditor().stopCellEditing();

在对话框按钮的动作监听器中。然后编辑模式被移除了,单元格得以更新。

谢谢!
Tobi

英文:

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

https://stackoverflow.com/questions/25771338/is-there-an-elegant-way-to-make-jtable-stop-editing-when-user-closes-the-applica

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

huangapple
  • 本文由 发表于 2020年10月25日 05:52:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64518342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定