3

我有一个使用 JTables 显示数据的应用程序,并且单元格是可编辑的,以便用户可以更改数据。用户还可以恢复更改,或从外部源加载数据。但是,如果用户使用键盘快捷键还原/加载数据,以便鼠标焦点不会从表格中移开,则当前选定的单元格不会被还原。事实上,刷新后,单元格进入编辑模式!然后,当用户离开此单元格时,会触发更改事件,因此旧值将被提交回数据存储。

我有一个简短的示例程序来演示这个问题。它显示了一个表格,其中每个单元格都显示值 0。还有一个文件菜单,其中包含一个名为“增量”的菜单项,其键盘快捷键为 Ctrl-I。每次调用 Increment 命令时,它都会增加所有单元格中显示的数字。要查看问题,请执行以下操作:

  1. 编译并运行程序
  2. 多次按 Ctrl-I 以调用 Increment 命令。请注意,单元格值每次都会增加。
  3. 单击一个单元格。
  4. 多次按 Ctrl-I 以调用 Increment 命令。请注意,所有单元格的值都会增加,除了选中的那个。

在刷新之前,我尝试了各种方法从表中删除选择,但无济于事。两者都不

table.editCellAt(-1, -1);

也不

table.getSelectionModel().clearSelection();

例如,工作。

这是示例程序:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableBug extends JFrame {
    private static final int ROW_COUNT = 3;
    private static final int COL_COUNT = 3;

    private int mDataValue = 0;
    private DefaultTableModel mTableModel;

    // Constructor
    public TableBug() {
        setTitle("TableBug");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        // Create table model and table
        mTableModel = new DefaultTableModel();        

        for (int col = 0; col < COL_COUNT; col++) {
            mTableModel.addColumn("Value");
        }

        JTable table = new JTable(mTableModel);
        setUpTable(table);
        refresh();

        // Create menu bar
        int keyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

        JMenu fileMenu = new JMenu("File");

        JMenuItem incrementMenuItem = new JMenuItem("Increment");
        incrementMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, keyMask));
        incrementMenuItem.addActionListener(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                doIncrement();
            }
        });

        fileMenu.add(incrementMenuItem);

        JMenuBar mainMenuBar = new JMenuBar();
        mainMenuBar.add(fileMenu);

        // Populate GUI
        setJMenuBar(mainMenuBar);
        add(new JScrollPane(table), BorderLayout.CENTER);

        // Display window
        pack();
        setVisible(true);
    }

    // Configures the table
    private void setUpTable(JTable table) {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        table.getTableHeader().setReorderingAllowed(false);
        table.getTableHeader().setResizingAllowed(false);

        table.setRowSelectionAllowed(false);
    }

    // Populates the table
    private void refresh() {
        mTableModel.setRowCount(ROW_COUNT);

        for (int col = 0; col < COL_COUNT; col++) {
            for (int row = 0; row < ROW_COUNT; row++) {
                mTableModel.setValueAt(mDataValue, row, col);
            }
        }
    }

    // Handles the Increment menu item
    public void doIncrement() {
        mDataValue++;
        refresh();
    }

    // Main program
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TableBug();
            }
        });
    }
}
4

1 回答 1

3

在您的刷新功能中,检查表是否正在被编辑。如果是,则获取正在编辑的行和列并停止单元格编辑。

private void refresh() {
    if (table.isEditing()) {
        int row = table.getEditingRow();
        int column = table.getEditingColumn();
        table.getCellEditor(row, column).stopCellEditing();
    }
 ...

为此,您需要使表变量可访问(使其成为类变量)。

于 2012-03-02T20:50:22.583 回答