问题
OK i am using eclipse and its GUI editor and i have a string like this:
public static String[] blah = {"Blah", "Blah", "Blah", "Blah"};
and a JComboBox like this:
JComboBox comboBox = new JComboBox(blah);
comboBox.setBounds(10, 56, 312, 37);
contentPane.add(comboBox);
The combobox uses the string above to get its data but when i enter in "blah" to the combobox it has this error...
Type safety: The constructor JComboBox(Object[]) belongs to the raw type JComboBox. References to generic type JComboBox<E> should be parameterized
it works if i run it because it is only a warning but it is annoying because it wont let me enter design mode unless i make it a comment. design mode gives this error...
INVALID SOURCE. No Constructor Binding. --- new JComboBox(locations) is not valid source for component creation, it references not existing constructor.
so i would like to know if there is any other way to overcome this issue
回答1:
The problem seams to be that WindowBuilder can't handle the generics version of JComboBox<E>
whitch is new in jre7.
I had the same problem and fixed it by adding the jre6 under Preferences -> Java -> Installed JREs and ensuring that the project uses the execution Environment 'JavaSE-1.6' under Project -> properties -> Java Compiler.
So if you don't need java 7 you can easily fix it, else you have to wait for Windowbuilder to support the generics version.
回答2:
// comboBoxTraceModeSelection = new JComboBox<TraceMode>(TraceMode.values());
comboBoxTraceModeSelection = new JComboBox<TraceMode>();
comboBoxTraceModeSelection.setModel(new DefaultComboBoxModel<TraceMode>
(TraceMode.values()));
This is a workaround for when using enum in a JComboBox (with WindowBuilder on Eclipse 3.7.2 for java 6). Yes, it does seem to be related to Java generics for objects that are a bit out of the ordinary (enum, String, etc). TraceMode is a custom enum. The commented-out line causes the same error as that of the original poster. This is a WindowBuilder issue, not an Eclipse or Java issue.
回答3:
I think OP already got an useful answer since 3 years are gone :D however, hope you'll find this useful :
Actually I don't know why we get this error but u can easily get rid of it by separing the ComboBoxModel creation and the JComboBox creation.
Defining first the model (e.g. using DefaultComboBoxModel), adding the needed elements and then passing the above model to JComboBox constructor, does not result in this error and works fine.
E.g.
public static String[] blah = {"Blah", "Blah", "Blah", "Blah"};
DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<String>(blah);
JComboBox comboBox = new JComboBox(comboModel);`
Quite concise and readable, I think :)
来源:https://stackoverflow.com/questions/8845139/jcombobox-warning-preventing-opening-the-design-page-in-eclipse