简体   繁体   English

JTable单元格在输入Click时未更新

[英]JTable Cell Isn't Updated On Enter Click

I have an app that has a JTable of some data.. So I add the data and it appears well.. But when I try to edit a cell, I can edit it but when I click enter or tab to save the new content it reflects as it was earlier.. I don't know why that happens.. I searched online but with no hope.. :( 我有一个应用程序,其中包含一些数据的JTable。.所以我添加了数据,它看起来很不错。反映了以前的情况..我不知道为什么会这样..我在网上搜索,但没有希望.. :(

Here is the Table Model class : 这是Table Model类:

public class TableModel extends AbstractTableModel {

String[] columnNames = { "Col1", "Col2", "Col3", "Col4", "Col5", "Col6" };

private List<Entry> db;
JTable table;

public TableModel(JTable table) {
    this.table = table;
}

public void addRow(Entry entry) {
    db.add(entry);
    fireTableRowsInserted(db.size() - 1, db.size() - 1);
    table.editCellAt(db.size() -1, 0);
}

@Override
public boolean isCellEditable(int row, int column) {
    return true;
}

public void setData(List<Entry> db) {
    this.db = db;
}

@Override
public String getColumnName(int column) {
    return columnNames[column];
}

@Override
public int getColumnCount() {
    return 6;
}

@Override
public int getRowCount() {
    return db.size();
}

@Override
public Object getValueAt(int row, int col) {
    Entry entry = db.get(row);

    try {
        switch (col) {
        case 0:
            return entry.getItem();
        case 1:
            return entry.getTransPayment();
        case 2:
            return entry.getEquipPayment();
        case 3:
            return entry.getGasPayment();
        case 4:
            return entry.getEquipSparePayment();
        case 5:
            return entry.getTransSparePayment();
        case 6:
            return entry.getTotal();
        }
    } catch (Exception e) {
        fireTableDataChanged();
    }


    return null;
}


}

The question is : Do I need to add any custom methods or code to do what I mentioned ? 问题是:我需要添加任何自定义方法或代码来完成我提到的事情吗?

I was hoping you could do this with a custom TableCellEditor , but that didn't turn out to be the case. 我希望您可以使用自定义TableCellEditor进行此操作,但事实并非如此。

You need to provide a key binding for the Enter key, which would allow you to associate an appropriate action for the key, for example... 您需要为Enter键提供一个键绑定,这将使您可以为该键关联一个适当的操作,例如...

public class EditSelectedCellAction extends AbstractAction {

    private JTable table;

    public EditSelectedCellAction(JTable table) {
        this.table = table;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int col = table.getSelectedColumn();
        int row = table.getSelectedRow();

        if (col >= 0 && row >= 0) {
            table.editCellAt(row, col);
        }
    }

}

You would then need to bind the EditSelectedCellAction to the Enter key, for example... 然后,您需要将EditSelectedCellAction绑定到Enter键,例如...

InputMap im = table.getInputMap(JTable.WHEN_FOCUSED);
ActionMap am = table.getActionMap();

KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
im.put(enterKey, "Action.enter");
am.put("Action.enter", new EditSelectedCellAction(table));

Feedback 反馈

There is never any reason why a model should ever have a reference to any view component, ever. 从来没有任何理由可以使模型永远引用任何视图组件。 The model should simply not care. 该模型应该根本不在乎。 A model could be associated with multiple views (in this case, tables), making in practical to maintain a reference to any view component. 一个模型可以与多个视图(在这种情况下是表)相关联,从而实际上可以维护对任何视图组件的引用。

Instead, what ever is calling your addRow method should be making the decisions to edit the cell. 相反,曾经调用addRow方法的应该是决定编辑单元格的决定。 Maybe, your addRow method could return the row index. 也许,您的addRow方法可以返回行索引。

Also, in you getValueAt method, you call fireTableDataChanged(); 另外,在您的getValueAt方法中,您调用fireTableDataChanged(); if, for some reason, something goes wrong, this could set up a nasty chain reaction which could place your program in an infinite loop and is best avoided 如果由于某种原因出了点问题,这可能会造成讨厌的连锁反应,从而使您的程序陷入无限循环,最好避免

Updated 更新

You know, at first, I though you hadn't overriden isCellEditable , but as it turns out, you've not override setValueAt 您知道,起初,我虽然没有覆盖过isCellEditable ,但事实证明,您没有覆盖setValueAt

@Override
public void setValueAt(Object aValue, int rowIndex, int colIndex) {
    Entry entry = db.get(rowIndex);
    switch (colIndex) {
        case 0:
            entry.getItem();
            break;
        case 1:
            entry.setTransPayment(aValue);
            break;
        case 2:
            entry.setEquipPayment(aValue);
            break;
        case 3:
            entry.setGasPayment(aValue);
            break;
        case 4:
            entry.setEquipSparePayment(aValue);
            break;
        case 5:
            entry.setTransSparePayment(aValue);
            break;
        case 6:
            entry.setTotal(aValue);
            break;
    }
    fireTableCellUpdated(rowIndex, colIndex);
}

Now, because you didn't provide the source for Entry , I need to guess at its data types and functionality 现在,由于您没有提供Entry的来源,因此我需要猜测其数据类型和功能

but when I click enter or tab to save the new content it reflects as it was earlier.. 但是当我单击Enter或Tab保存新内容时,它会像以前一样反映。

Your TableModel doesn't implement the setValueAt(...) method so the data from the editor is never saved. 您的TableModel没有实现setValueAt(...)方法,因此永远不会保存来自编辑器的数据。

@Override
public void setValueAt(Object value, int row, int column)
{
    Entry entry = db.get(row);

    switch (column)
    {
        case 0: entry.setItem(value); break;
        case 1: entry.setTransPayment(value); break;
        ...
    }

    fireTableCellUpdated(row, column);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM