Simple. Now run the app win1 opens, I click button to open win2 - today's date (Feb 6 2009) is shown. Now I change the month to December or whatever, click the cancel button to dispose win2. Without exiting the app, in win1 click button to reopen win2. I see December still selected instead of February. This happens for all 3 combo boxes.
Since I already explicitly set the combo box's items/values accordingly. Why does it have the previous selected state (December)? Or is there something I should know about disposing windows? How to guarantee today's date is shown?
Since I opening win2 from win1's button (using action listener), I did "new MyListener()" anyway, won't this be enough?? Also I'm using a JTable and every window I open I'm passing an instance of JTable.
If I didn't pass a JTable, then my file will have tons of inner classes.
K. Tsang wrote:Thanks Rob for your suggestion. But first I did dispose the combo box frame (win2) when I click the buttons because my win2 extends JDialog. Won't that clear the state? Or does JDialog keep states and JFrame doesn't??
With disposing I meant give up its reference, and let it be garbage collected. Calling dispose() on a frame or dialog does not clear its state.
K. Tsang wrote:run the app win1 opens, I click button to open win2 - today's date (Feb 6 2009) is shown. Now I change the month to December or whatever, click the cancel button to dispose win2. Without exiting the app, in win1 click button to reopen win2. I see December still selected instead of February.
If you really want to dispose win2 and not reuse the same instance of win2, the way to handle this is to reuse the ComboBoxModel when you recreate the combo boxes on win2.
DefaultComboBoxModel yearModel = new DefaultComboBoxModel(...);
// yearModel.addElement(...) , yearModel.setSelectedItem(...), etc.
DefaultComboBoxModel monthModel = new DefaultComboBoxModel(...);
DefaultComboBoxModel dayModel = new DefaultComboBoxModel(...);
Then when it's time to create a new win2:
JComboBox yearComboBox = new JComboBox(yearModel);
JComboBox monthComboBox = new JComboBox(monthModel);
JComboBox dayComboBox = new JComboBox(dayModel);
It's probably easier to just reuse win2. (Hide it without disposing it.)