拦截jTable选择更改事件 [英] Intercept jTable selection change events

查看:112
本文介绍了拦截jTable选择更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现此论坛主题,该主题建议重写ListSelectionModel以防止选择行.

I found this forum thread which suggests overriding ListSelectionModel to prevent rows from being selected.

当用户确认放弃之前,当前选择的项目有未保存的更改(在表格外部)时,我想防止选择更改.像这样:

I would like to prevent selection changes when there are unsaved changes (external to the table) for the currently selected item UNTIL the user confirms the discard. Something like:

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;
        }
    }
}

是否可以在ListSelectionModel更改的中间插入阻止代码?有没有更好的方法来拦截选择更改事件?

Is it possible to insert blocking code in the middle of ListSelectionModel changes? Is there a better way to intercept selection change events?

我已经在听他们了,但是那时改变已经发生了.

I'm already listening for them but the change has already happened by then.

推荐答案

我的最终解决方案(部分感谢

My final solution (thanks in part to this code guru) was to create an anonymous inner class that extends JTable and overrides changeSelection(). Tried a separate class since I read that some people don't think anonymous inner classes are good OO design but I needed to know about the editing state plus I had to call save/discard methods. Who needs encapsulation when it's your own code anyway? ;-)

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);
    }
};

到目前为止,该解决方案似乎仍然有效,但我尚未对其进行广泛的测试.在此代码的黑暗角落之一中可能存在漏洞或陷阱……

This solution seems to work so far but I haven't tested it extensively. There may be bugs or gotcha's lurking in one of the dark corners of this code...

希望它对其他人有帮助!

Hope it helps someone else!

这篇关于拦截jTable选择更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆