0

In my application, in which I have two JLists, I'm trying to show the contents of the second one depending on what is selected in the first one. Let's say I have group 1, group 2 and group 3 at List1, and when I select Group 1, I want List2 to display what I have at Group 1, and so on. I have actually coded it, and it works, but it gives this warning:

[unchecked] unchecked call to setModel(ListModel<E>) as a member of the raw type JList
            exampleGroup2.setModel(group2Container.get(exampleGroup1.getSelectedIndex()));
  where E is a type-variable:
    E extends Object declared in class JList
1 warning

I've read similar questions, like this one, about generics and so, and they resolved similar problems which gave a similar warning, in which I had to specify the <String> type, but it doesn't help here.

The line itself, where the warning pops out, is this one:

exampleList2.setModel(list2Container.get(exampleList1.getSelectedIndex()));

Where everything is declarated as:

DefaultListModel<String> list1Container = new DefaultListModel<>();
ArrayList<DefaultListModel<String>> list2Container = new ArrayList<>();
list2Container.add(new DefaultListModel<>());

JList exampleList1 = new JList<>((ListModel<String>) list1Container);
JList exampleList2= new JList<>((ListModel<String>) list2Container.get(0));

Sorry if it's a bit messy, I've tried to make it as short and clear as possible. As I said, I've looked on generics, and so, but I cannot find where should I put the <String>, or even what do I have to put, in this case.

Thank you very much, for your help.

  • Just like all the similar questions, you need to declare your JList type. So `JList exampleList1 = new JList<>(....);`, and same for exampleList2. – Hovercraft Full Of Eels Jan 12 '18 at 15:00
  • Shoot, your link already tells you this -- why didn't you just follow what the answer tells you to do? – Hovercraft Full Of Eels Jan 12 '18 at 15:09
  • As the warning was about the setModel() method, and also it specified that line, I thought the problem was there... Also, didn't thought JList needed to specify a type (as every other type of list, I realize now...). Anyway, thank you very much, it solved the problem. – Hector Guerra Jan 12 '18 at 15:18

0 Answers0