Hi, everyone! I create a JComboBox object that contains several items, this JComboBox is displayed in a JDialog oject. The user can select the items when the JDialog object is displayed. And I discover that when user close the dialog then open the dialog again, the default display item is not the one the user selected last time. I want to display the item that user select last time in a JComboBox as a default item. How can I do that?
What you discovered is the correct way it should work. There is no reason a JComboBox should remember your last selection, especially when you have destroyed the object (by destroying the JDialog) and created a brand new object when you created a new JDialog.
You have a couple of options here.
1. Save the selected item in some way (memory, properties file, etc) and then load that item when you open a new JDialog and set the JComboBox appropriatly.
2. When you "close" the JDialog, do not call dispose(), just setVisible(false). Then, when you go to open a new one, don't do "new" just do setVisible(true).