2009-10-12 71 views
2

當前,我有一個JList監聽列表選擇監聽器。在選擇之前驗證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選擇時,已經太晚了。

回答

3

我已經爲相同的工作流程用例實施瞭如下操作。雖然它對我來說足夠有效,但我確實希望有一種更簡單和更優雅的方法,在選擇活動可能會在繼續之前被否決。如果我有時間進行調查並找出結果,我會重新發布,但它可能會成爲投資回報不值得(即定製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... 
} 
} 
+0

得到你!由於時間不夠,我正在使用類似的實現方式。 – 2009-10-28 03:44:54

0

我認爲你需要重寫JList的setSelectionInterval(...)方法,以便在你的特殊情況下不做任何事情。

在事件發生時處理它已經太晚了。

+0

但setSelectionInterval旁邊,仍然有許多JList的其他選擇方法。 – 2009-10-13 04:12:00

+0

然後,您將需要重寫多個方法。 – camickr 2009-10-13 04:47:20

0

我建議您實施自定義ListSelectionModel

+0

您能否提供一個具體的例子來說明如何去做?因爲我有0的想法需要做什麼。 – 2009-10-13 03:51:38

+0

嘗試'VetoableListSelectionModel'從http://stackoverflow.com/questions/7936064/before-cell-select-jtable-event – xmedeko 2013-11-12 12:32:26

0
 
    table.setSelectionModel(new DefaultListSelectionModel(){ 

    @Override 
    public void setSelectionInterval(int index0, int index1) { 

    if (dragState==0 && index0==index1 && isSelectedIndex(index0)) { 
    // Deny all clicks that are one row & already selected 
    return; 
    } else { 
    super.setSelectionInterval(index0, index1); 
    } 
    } 

    }); 
相關問題