1
if(incasationBegin > 0)
{
  int anwser = JOptionPane.showConfirmDialog(null, Config.QUESTION,"Confirm", JOptionPane.YES_NO_OPTION);
  if(anwser == 1)
  {
      jList0.setSelectedIndex(incasationBegin);
      return;
  }
}
incasationBegin = jList0.getSelectedIndex();

How do I setSelectedIndex without calling jList0ListSelectionValueChanged action? Because when I click on option confirm popup and when I click NO, the new item is still selected. I have tried to add incasationBegin =0; before return, but then on first click confirm popup.

skaffman
  • 398,947
  • 96
  • 818
  • 769
kskaradzinski
  • 4,954
  • 10
  • 48
  • 70
  • 1
    Even with the code and explanation, I still don't follow you. Can you please elaborate or make your explanation more clear/concise? – mre May 11 '11 at 13:45
  • after calling `jList0.setSelectedIndex(incasationBegin);` this the action `jList0ListSelectionValueChanged` is called, and I dont want that, is it possible ?? – kskaradzinski May 11 '11 at 13:49
  • Well, `valueChanged` is triggered whenever selection/deselection is done. So, not wanting this to happen seems very unnatural. Anyway, a little bit of browsing landed me the following link - http://stackoverflow.com/questions/3092834/can-i-set-the-selected-item-for-a-jlist-without-having-an-event-thrown-to-the-lis – mre May 11 '11 at 14:01

1 Answers1

2

Let me see if i understood you correctly. You are adding a ListSelectionListener to the JList and want to prevent your call to setSelectedIndex from firing the valueChanged event, is that it?

You can try a lot of different approaches here:

  1. Delay your call to jList0.addListSelectionListener(... in such way that no Listener exists when you call setSelectedIndex.
  2. Have the listener valueChanged method check for some "enabled condition", for example read a boolean isEnabled. Set this condition to false before calling setSelectedIndex and to true after that.
  3. Call jList0.removeListSelectionListener(.. before the call to setSelectedIndex. Add the listener to the list again after the call.
Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118