I'm adding an ItemListener to a combobox and I want to know if the event ItemEvent.DESELECTED is always triggered BEFORE the event ItemEvent.SELECTED, or if the order is unspecified.
On a simple example, it seems that DESELECTED is fired before, but can I trust this example?
combo = new JComboBox<String>(new String[}{"A","B","C"});
combo.addItemListener(new ItemListener()
{
@Override
public void itemStateChanged(final ItemEvent e)
{
if(e.getStateChange() == ItemEvent.SELECTED)
{
logger.debug("{} selected", e.getItem());
// Load the file selected in the JTextArea using JTextArea.setDocument()
}
else if(e.getStateChange() == ItemEvent.DESELECTED)
{
logger.debug("{} deselected", e.getItem());
// If doc in the textarea has been modified, save it now
}
}
});
To add context to the question, I use the combo to select a file to load in a JTextArea. The user can edit the file via the textarea. If she selects another file in the combo, I want to inform her that the previous file has been edited and ask her confirmation to save the edits. Can I implement this safely in the ItemListener of the combo? By safely, I mean can I be sure that if I call textArea.getText() at this point in time (item DESELECTED), am I sure that the textarea is not already filled up with the content of the new file to load?