user3155478
user3155478

Reputation: 1055

JCombobox - no success adding 2d String-array when parameterized

If I instanciate the JCombobox-class of Swing and add String[][]-items to it - there is no problem except that I get the following warning:

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

Well - I parameterize the object in the following manner

private JComboBox <String[][]> myComboBox = new JComboBox <String[][]> ();

It looks good at first because the warning dissappears, but I get an error when I want to add items from the String[][]-object

 myComboBox.addItem(this.stringList[i][1]);

errormessage:

 *the method addItem(String[][]) in the type JComboBox <String[][]> is not applicable for the arguments (String).*

What did I do wrong and how do I solve it?

By the way - if you have time to answer more - Is there a danger/drawback using rawtypes?

Upvotes: 0

Views: 432

Answers (1)

i_turo
i_turo

Reputation: 2729

Well, when you parametrize JComboBox with <String[][]>, the method addItem() requires a String[][] as a parameter:

public void addItem(String[][] item) { // method signature when <String[][]>

In your example you are trying to pass a normal String, which is obviously not allowed, since it is not a String[][].

To fix this issue, just parametrize it with String:

private JComboBox<String> myComboBox = new JComboBox<>();

For more information about rawtypes see the amazingly detailled answer to that question:
What is a raw type and why shouldn't we use it?

Upvotes: 6

Related Questions