Check JList before choosing
I currently have a JList listening on a list listener.
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?
*/
}
}
In a certain situation, I can prevent the user from choosing other than the current selection. How can i do this?
It seems to me too late when I receive the ListSelectionEvent. But if I want to do this before the ListSelectionEvent appears, I don't know that the user is trying to select another one.
Here is one of the senario.
JList contains a list of the project name. Thus, whenever the user selects a new list item, we need to rotate the view from the current project and display the new project. However, the current project may not be saved. Therefore, if the current project is not saved, we will ask the user for confirmation "Save project"? (Yes, No, Cancel) When the user chooses to cancel, it means he wants to cancel the "select another project" action. He wants to stick with the current JList selection. A confirmation dialog box appears in the jList1ValueChanged event handle. But when we try to stick with the current JList selection, it's too late.
source to share
I have implemented it as follows for the same workflow use case. While it works enough for me, I wish there was a simpler and more elegant approach where the select event could be vetoed before proceeding. If I have time to research and understand this, I'll retell it, but it might mean that the return on investment isn't worth it (i.e., customize Swing classes, handle down-level mouse / keyboard events directly, etc.). Anyway, what I am currently doing is saving the last good "checked" choice and reverting back to it if the user cancels a future choice. This is admittedly not the nicest solution, but it works:
// 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...
}
}
source to share
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); } } });
source to share