Restore JComboBox

J

Jason Cavett

I am wondering if there is a nice easy way to restore a JComboBox
selection (other than the obvious).

Basically, I have placed an ActionListener on a JComboBox. However,
when the listener is fired (which happens after the new selection has
been made), I have to ask the user some questions. If they choose not
to go through with the change, I want to revert back to the previous
value. Is there a way to do this that is better than keeping around a
reference that stores the current selection at all times?

I looked for a listener that capture the change *before* it went
through, but I haven't seen anything.

Thanks,
Jason
 
D

Daniele Futtorovic

I am wondering if there is a nice easy way to restore a JComboBox
selection (other than the obvious).

Basically, I have placed an ActionListener on a JComboBox. However,
when the listener is fired (which happens after the new selection has
been made), I have to ask the user some questions. If they choose not
to go through with the change, I want to revert back to the previous
value. Is there a way to do this that is better than keeping around a
reference that stores the current selection at all times?

I looked for a listener that capture the change *before* it went
through, but I haven't seen anything.

ItemListener. You'll get notified when an item is deselected.

Another way would be to extend JComboBox and play a bit with the
internals. Bit more dodgy though.
 
M

Mark Space

Daniele said:
Another way would be to extend JComboBox and play a bit with the
internals. Bit more dodgy though.

This might be my first choice. NetBeans allows you to import components
(Beans) to the GUI designer palette. It's pretty trivial to derive a
new component by extending an existing one.

I'd add a new property "default" which gets set by the init code, then a
method called "revertToDefault" which a listener can call to change the
sate back to the previously set default. A bit of custom init code in
the GUI could set this up for you, then just have any given listener
call "revertToDefault" on the check box when appropriate.
 
D

Daniele Futtorovic

This might be my first choice.

If I couldn't get it done via listeners, mine would be to write a
specific ComboBoxModel with a SelectionValidator or something like that.
NetBeans allows you to import components
(Beans) to the GUI designer palette. It's pretty trivial to derive a
new component by extending an existing one.

I'd add a new property "default" which gets set by the init code, then a
method called "revertToDefault" which a listener can call to change the
sate back to the previously set default. A bit of custom init code in
the GUI could set this up for you, then just have any given listener
call "revertToDefault" on the check box when appropriate.

Urgh. GUI designers.
 
J

Jason Cavett

ItemListener. You'll get notified when an item is deselected.

Another way would be to extend JComboBox and play a bit with the
internals. Bit more dodgy though.

Thank you! Worked very nicely.
 
D

Daniele Futtorovic

Come on, you know you want to. All the other kids are doing it.

Hmmmm... So in your opinion there's a similarity between GUI design
engines and masturbation? Interesting thesis.
 
M

Mark Space

Daniele said:
Hmmmm... So in your opinion there's a similarity between GUI design
engines and masturbation? Interesting thesis.

I was thinking "drink beer." I'll let the reader work out the rest of
this post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top