【问题标题】:Validate JList Before Selection Occur在选择发生之前验证 JList
【发布时间】:2009-10-12 19:10:32
【问题描述】:

目前,我有一个 JList 监听列表选择监听器。

private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {
    // When the user release the mouse button and completes the selection,
    // getValueIsAdjusting() becomes false        
    if (evt.getValueIsAdjusting()) {
        /*
          In certain situation, I may want to prevent user from selecting other
          than current selection. How can I do so?
        */
    }
}

在某些情况下,我可能希望阻止用户选择当前选择以外的选择。我该怎么做?

当我收到 ListSelectionEvent 时似乎为时已晚。但是,如果我想在 ListSelectionEvent 发生之前这样做,我不知道用户正在尝试选择其他人。

这是其中的一种。

JList 是包含项目名称的列表。 因此,每当用户选择新的列表项时,我们需要从当前项目中打开视图,并显示新项目。 但是,当前项目可能尚未保存。 因此,如果当前项目尚未保存,我们将要求用户确认,“保存项目?” (是,否,取消) 当用户选择取消时,这意味着他想取消他的“选择另一个项目”动作。他想坚持当前的 JList 选择。 我们将在 jList1ValueChanged 事件句柄中弹出确认对话框。 但是当我们试图坚持当前的 JList 选择时,已经为时已晚。

【问题讨论】:

    标签: java


    【解决方案1】:

    对于相同的工作流用例,我已按如下方式实现。虽然它对我来说足够有效,但我确实希望有一种更简单、更优雅的方法,在继续之前可以否决选择事件。如果我有时间调查并弄清楚我会重新发布,但它可能会被列为投资回报不值得的情况(即自定义 Swing 类、直接处理较低级别的鼠标/键盘事件等)。无论如何,我目前正在做的是保存最后一个好的“验证”选择,如果用户取消未来的选择,则恢复它。诚然,这不是最漂亮的解决方案,但它确实有效:

    // save the last good (i.e. validated) selection:
    private ProjectClass lastSelectedProj;
    
    // listing of available projects:
    private JList list;
    
    // true if current selected project has been modified without saving:
    private boolean dirty;
    
    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent evt) {
    
        if (evt.getValueIsAdjusting()) return;
    
        // first validate this selection, and give the user a chance to cancel.
        // e.g. if selected project is dirty show save: yes/no/cancel dialog.
        if (dirty) {
            int choice = JOptionPane.showConfirmDialog(this,
                "Save changes?",
                "Unsaved changes",
                JOptionPane.YES_NO_CANCEL_OPTION,
                JOptionPane.WARNING_MESSAGE);
    
            // if the user cancels the selection event revert to previous selection:
            if (choice == JOptionPane.CANCEL_OPTION) {
                dirty = false; // don't cause yet another prompt when reverting selection
                list.setSelectedValue(lastSelectedProj, true);
                dirty = true;  // restore dirty state. not elegant, but it works.
                return;
            } else {
                // handle YES and NO options
                dirty = false;
            }
        }
    
        // on a validated selection event:
        lastSelectedProj = list.getSelectedValue();
    
        // proceed to update views for the newly selected project...
    }
    }
    

    【讨论】:

    • 知道了!我正在使用类似的实现,因为我的时间不多了。
    【解决方案2】:

    我认为您需要覆盖 JList 的 setSelectionInterval(...) 方法,以便在您的特殊情况下不执行任何操作。

    在事件级别处理它为时已晚,因为事件已经发生了。

    【讨论】:

    • 但是除了setSelectionInterval之外,JList中还有很多其他的选择方法。
    • 那么您将需要覆盖多个方法。
    【解决方案3】:

    我建议你实现一个自定义的ListSelectionModel

    【讨论】:

    【解决方案4】:
    table.setSelectionModel(new DefaultListSelectionModel(){ @覆盖 公共无效 setSelectionInterval(int index0,int index1){ if (dragState==0 && index0==index1 && isSelectedIndex(index0)) { // 拒绝所有单行且已被选中的点击 返回; } 别的 { super.setSelectionInterval(index0, index1); } } });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多