NetBeans - JComboBox warning - missing type arguments for generic class JComboBox<E>
Asked Answered
C

2

12

I am using NetBeans IDE 7.1.2. When I compile my application I get the following warning:

warning: [rawtypes] found raw type: JComboBox city = new javax.swing.JComboBox(); missing type arguments for generic class JComboBox where E is a type-variable: E extends Object declared in class JComboBox

So, I guess I have to declare the JComboBox as:

JComboBox<String> city = new JComboBox<String>();

But how do I do this in NetBeans, using the Matisse (Swing GUI Builder)? Please help.

Contingence answered 9/8, 2012 at 15:19 Comment(0)
S
17

In Netbeans 7.2 you can click on Code section for JComboBox, and then write type into "Type Parameters", in your case: <String>.

Snob answered 20/12, 2012 at 19:11 Comment(2)
This helped, just one small improvement to clarify how to get to the code section: right-click the JComboBox, choose Properties, select the Code section (at the top of the properties window). Type Parameters is close to the top.Yearlong
@chr and remember to write the type parameter including the angle brackets.Gunas
R
1

Java 7 introduced generics to the JComboBox. One solution to your problem would be to ust Java 6.

I'd bet the latest version of Netbeans (7.2) will have a solution for this (although I'm not positive).

Otherwise, if I remember right, you can view the code generated by Netbeans. If so, you may be able to add the generic arguement yourself. It's been many months since I tinkered with Netbeans though...

Also, if the Netbeans allows you to, you can add the @SupressesWarnings annotation above the JComboBox declaration (or even above the class declaration, although that changes it's scope). It would be something like this:

@SuppressWarnings("rawtypes")
JComboBox city = new JComboBox();

There are lots of options, but Netbeans may hold you back from implementing some of them.

Ratite answered 9/8, 2012 at 15:47 Comment(3)
Nick I tried to change the code to: city = new javax.swing.JComboBox<String>(); (NetBeans does not allow me to change the declaration variables therefore my city is defined as private javax.swing.JComboBox city;). When I did this I get the following warning: redundant type arguments in new expression (use diamond operator instead).Contingence
I'm not sure you'll be able to get around the warning then without downgrading your Java version or updating your Netbeans IDE. Fortunately, it's just a warning so it won't affect your actual code. If it bothers you, you can try adding this line above your declaration: @SuppressWarnings("rawtypes") I'll add this to the answer as well. I don't know of a way to turn compiler warnings off in Netbeans - someone else may know.Ratite
Thank you very much Nick. At least now I know that I am not doing anything wrong as per coding. I hope this is fixed in the v7.2, I haven't tried it yet, waiting for oracle to release it with JDK.Contingence

© 2022 - 2024 — McMap. All rights reserved.