0

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?

user3155478
  • 975
  • 1
  • 10
  • 16

1 Answers1

6

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?

Community
  • 1
  • 1
i_turo
  • 2,679
  • 1
  • 13
  • 15
  • I assume `stringList` is a `String[][]` so it contains `String`s. When doing `stringList[i][1]` you get the `String` in row *i* and column *1*. If you really want to add the whole array just do `add(stringList);`. The error message actually says that you are trying to add a `String`. – i_turo Jul 30 '14 at 19:04