• Post Reply Bookmark Topic Watch Topic
  • New Topic

reseting combo box values  RSS feed

 
K. Tsang
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I have 2 frames call win1 and win2. Win1 has a button to call win2 that has some JComboBox that represents dates.


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?

Any ideas?
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're using the same instance, which remembers its state. Either dispose the second frame and create a new one each time (a new state is created for the new frame), or reset the date either just before you open the second frame (before setVisible(true)) or just after the second frame is closed (use a WindowListener or perhaps a ComponentListener).
 
K. Tsang
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??

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.
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Something like that ?


> Why does it have the previous selected state (December)?

Well do you have some listener for combo boxes (as you said) and you are there setting these currentYear, etc... values to new, if some item gets selected in combo box !
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mikko, I will try that and let you know.
 
K. Tsang
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I got it to work now. in my so-called date combo box window, instead of extending JDialog, i changed it to local variable and used anonymous inner class to take care of the buttons.
 
Brian Cole
Author
Ranch Hand
Posts: 936
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
win2.add(yearComboBox);
JComboBox monthComboBox = new JComboBox(monthModel);
win2.add(monthComboBox);
JComboBox dayComboBox = new JComboBox(dayModel);
win2.add(dayComboBox);

It's probably easier to just reuse win2. (Hide it without disposing it.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!