3

I have a class such that it extends a Swing component such as:

public class MyCustomClass extends JComboBox

The problem here is that I get the compiler warning:

JComboBox is a raw type. References to generic type JComboBox should be parameterized

I'm not sure to parameterize the JComboBox so that whatever class further extends from here can use any type of object. I've tried to put it as extends JComboBox, and so on, but that doesn't work. Any suggestions would be appreciated.

3
  • See also this related example. Commented Oct 5, 2012 at 1:25
  • You typically do not need to a extend the Jxxx classes (except for custom painting, I cannot think of any valid use case). They were designed to be used, and have API to customize them. And not extending them would solve your problem as well Commented Oct 5, 2012 at 5:49
  • The main reason you need to do this is if you want the same styling to apply on all the classes. For example we want all JComboBoxes to be opaque, we set the prototypeDisplayValue to be consistent, the maximum row count, etc. Basically we want to set it up to offer a consistent UI L&F without having to define it everywhere. And if something is changed it's changed at one location only. Commented Oct 5, 2012 at 22:39

4 Answers 4

6

if there are no restrictions on the type parameter for the JComboBox, then you can go with:

public class MyCustomClass<T> extends JComboBox<T>

If I remember the syntax correctly.

Sign up to request clarification or add additional context in comments.

Comments

4

The generics effect the type of data expected to be in the model. This is a neat feature in the long term, but may mean some short term pain.

You have two options that I can think of.

Firstly, construct you custom class so it allows the generics to fall through...

public class MyCustomClass<E> extends JComboBox<E>

Secondly, pass an Object as the data type for the combo box

public class MyCustomClass extends JComboBox<Object>

(or thirdly, constrain the custom combo box to a pre-defined data type for the purpose it was build for)

2 Comments

I guess, both approches are not available for Java 6?
@guitar_freak No, Java 6 doesn't support generics in the Swing components
3

The parameter is the type of object you want in the ComboBox.

Your choices are:

A: MyCustomClass always expects the same type of object, so you do something like:

public class MyCustomClass extends JComboBox<String>

OR

B: MyCustomClass still isn't "deep enough" down the tree to know the type of class it will work on. So you do:

public class MyCustomClass<T> extends JComboBox<T>

Comments

2

JComboBox became a generic class in Java 7, e.g. you should tell the compiler what type of objects are inside the JComboBox instance. so you can either make you class generic, or specify the type of the JComboBox.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.