Re: Simple question about Java Methods
On Aug 19, 11:01 am, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
Nigel Wade wrote:>
Why use Strings in the JComboBox? Enums can do pretty much all the work=
for you.
You can use any Object, including Enums (Waves in this case), in a JCom=
boBox.
JComboBox uses an Object's toString() method to display the text in the=
visual
component. Rather than using the JComboBox.getText() method use the
getSelectedItem() and you get back the selected Waves item that you act=
ually
want.
If you capitalize the Waves:
public enum Waves { Sine, Square, Sawtooth; }
the capitalized strings will be displayed in the JComboBox.
Or, an even more flexible approach, which doesn't require breaking
conventions:
public enum Waves {
Sine("Sine Wave"),
Square("Jack-o-lantern"),
Sawtooth("Evil Jack-o-lantern");
private final String displayName;
Waves(String displayName) { this.displayName = displayNames; }
public String toString() { return displayName; }}
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
So like if I do:
switch (Wavetype.valueOf(s))
but the current value of s is not included in the wavetype, it throws
an exception rather than go to default. How do I handle such cases
with enums? That is, why does it not simply go to "default:" and skip
over the cases? Can Wavetype.valueof(s) not return null and the null
case go to default in the switch?
Thanks