Error I get when compiling using Xlint is:

where E is a type-variable
E extends Object declared in class JComboBox
waring: [unchecked] unchecked call to addItem("blank") as a member of the raw type JComboBox

            new ActionListener()
                public void actionPerformed(ActionEvent e)

Recommended Answers

All 4 Replies

Looks like you declared your combo box without specifying the type, eg

JComboBox faceCombo = new JComboBox();

That means that Java cannot check the validity of anything you put into the combo box.
Since Java 1.5 you can specify the type of objects tha the combo box conatins, eg

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

so Java knows that all the entries are (must be) Strings, and it can check all your add methods to make sure that it is right.

You probably declared your faceCombo something like:

JComboBox faceCombo = new JComboBox();

Even though it will work, you used a raw type: JComboBox can take a type parameter that specifies what the type is of the items you want to put in it.

From the code snippet you posted it seems like you want to put items of type String in your combo box, so you'll want to specify String as argument to the type parameter, as follows:

// Pre Java 7:
JComboBox<String> faceCombo = new JComboBox<String>();

// Since Java 7 you can also use diamond syntax to let the compiler
// infer the type on the right hand side.
JComboBox<String> faceCombo = new JComboBox<>();

So, what does this stuff really mean?

If you write

JComboBox faceCombo = new JComboBox();

it is equivalent to specifying Object as type argument:

JComboBox<Object> faceCombo = new JComboBox<Object>();

In Java every class either directly or indirectly and explicitly or implicitly extends class Object.
That means that if you have Object as type argument you can really put every object in it.
If you are not careful, and somewhere in your code you'd accidentally add an object of type Integer in it, then your combo box model will contain objects of two different inheritance trees (only having Object as supertype in common).

// implicitly Object is the argument to the type parameter, DON'T DO THIS
JComboBox faceCombo = new JComboBox();

// the following is now possible (you don't want this)
faceCombo.addItem(5); // 5 is type int, however it will be autoboxed into an Integer
// The ability of mixing Strings with Numbers and any other reference types has potential
// for bugs !!!

You'll get a compiler warning about this, it is not an error, but it is always a good idea to treat these seriously because they warn you for potential bugs you could encounter if you choose a particular approach.

So, if you use a raw type the only thing the compiler can do for you is check if the things you're putting in your combobox IS AN Object.
But in Java every class extends Object, so the compiler can't guarantee that somewhere in your code you'll make the mistake of putting an Object of another type in it.

What you really want is a mechanism that helps you in catching these kind of mistakes.
The compiler provides you with one, but if you use raw types, you defeat these type-checks.
Therefore you can specify arguments to the type parameters to allow type checking by compiler, like this:

// specify String as type argument
JComboBox<String> faceCombo = new JComboBox<String>();

// add items
faceCombo.addItem(5); // this line will now give you a compiler error

Points to remember:

  • Whenever you can specify type arguments, do so.
  • Working with raw types defeats the compiler's type checking mechanism.

Thank you, Its fixed my problem.

Excellent. Please mark this thread "solved" for our knowledge base.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.