Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 截获jTable选择更改事件_Java_User Interface_Swing_Jtable_Selection - Fatal编程技术网

Java 截获jTable选择更改事件

Java 截获jTable选择更改事件,java,user-interface,swing,jtable,selection,Java,User Interface,Swing,Jtable,Selection,我发现这建议重写ListSelectionModel以防止选中行 在用户确认放弃之前,如果当前所选项目存在未保存的更改(表外部),我希望阻止更改选择。比如: public class confirmSelectionChange extends DefaultListSelectionModel { public void setSelectionInterval(int index0, int index1) { if (unsavedChanges()) {

我发现这建议重写ListSelectionModel以防止选中行

在用户确认放弃之前,如果当前所选项目存在未保存的更改(表外部),我希望阻止更改选择。比如:

public class confirmSelectionChange extends DefaultListSelectionModel {
    public void setSelectionInterval(int index0, int index1) {
        if (unsavedChanges()) {
            super.setSelectionInterval(int index0, int index1);
        }
    }

    private boolean unsavedChanges() {
        if (noUnsavedChangesExist) {
            return true;
        }

        // Present modal dialog: save, discard cancel
        if (dialogAnswer == SAVE) {
            // save changes
            return true;
        } else if (dialogAnswer == DISCARD) {
            return true;
        } else {
            return false;
        }
    }
}

是否可以在ListSaleMeC改模型中间插入阻塞代码?是否有更好的方法拦截选择更改事件

我已经在监听它们了,但更改已经发生了。

我的最终解决方案(部分原因是)是创建一个匿名内部类,该类扩展JTable并重写
changeSelection()
。我尝试了一个单独的类,因为我读到一些人不认为匿名内部类是好的OO设计,但我需要知道编辑状态,并且我必须调用save/discard方法。当它是您自己的代码时,谁还需要封装?;-)

到目前为止,这个解决方案似乎有效,但我还没有对它进行广泛的测试。这段代码的一个黑暗角落里可能潜藏着bug或gotcha


希望它能帮助别人

抱歉,如果询问、回答和评论的形式不好,但我只是想补充一点,在我的实际程序中,我将上述对话框代码移到了自己的方法中,因为在其他情况下,我需要相同的保存/放弃/取消功能(例如退出时)。
jTableMemberList = new JTable() {
    public void changeSelection(int rowIndex, int columnIndex, boolean toggle,
                                boolean extend) {
        // Member is being edited and they've clicked on a DIFFERENT row (this
        // method gets called even when the selection isn't actually changing)
        if (editModeIsActive && getSelectedRow() != rowIndex) {
            // User was editing, now they're trying to move away without saving
            Object[] options = {"Save", "Discard", "Cancel"};
            int n = JOptionPane.showOptionDialog(this,
                                            "There are unsaved changes for the "
                                            + "currently selected member.\n\n"
                                            + "Would you like to save them?",
                                            "Save changes?",
                                            JOptionPane.YES_NO_CANCEL_OPTION,
                                            JOptionPane.WARNING_MESSAGE,
                                            null,
                                            options,
                                            options[0]);

            if (n == JOptionPane.YES_OPTION) {
                saveChanges();
            } else if (n == JOptionPane.NO_OPTION) {
                discardChanges();
            } else {
                // Exit without passing call on to super
                return;
            }
        }

        // make the selection change
        super.changeSelection(rowIndex, columnIndex, toggle, extend);
    }
};