Search code examples
javacomboboxunsafeoperations

Java Unsafe Operations with Combo Boxes


I know this question has been asked before, but I cannot find a solution to the issue of combo boxes. I have the following code:

...
JComboBox startingCombo = new JComboBox();
startingCombo.setModel(new DefaultComboBoxModel(new String[] {"USD", "EUR", "GBP"}));
...

I get an error when running from commandline:

Note: ConverterFrame.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

When I do recompile with -Xlink:Unchecked I receive several of the following errors:

ConverterFrame.java:88: warning: [unchecked] unchecked call to DefaultComboBoxModel(E[]) as a member of the raw type DefaultComboBoxModel

This line then points to "new" on the second line in the code provided. I understand that I need to add an object type, but I am unsure where to add it and how.


Solution

  • Both JComboBox and DefaultComboBoxModel have a generic type parameter for the type of their items. You want String items therefore write

    JComboBox<String> startingCombo = new JComboBox<>();
    startingCombo.setModel(new DefaultComboBoxModel<String>(new String[] {"USD", "EUR", "GBP"}));