1 Reply Latest reply: Mar 14, 2014 5:07 PM by rp0428 RSS

    Adding elements to an existing JList under Java7

    02e467d2-695d-4f17-98f3-d9032cfd776f

      I know there was a change in the later versions of Java where the C++ equivalent of a <template type> was added. Unfortunately the change has 'broken' my older code. If I have a JList and I want to add elements to it then now I should specify the type e.g., the list will store Strings. When I do this and then add data to the list (or actually the list model) the code is ''fixed". However if after adding those new elements to the list I later need to add more elements, which isn't unreasonable for a list...to have elements added dynamically at run time then I again get the same compiler error message that I haven't correctly specified the type:

       

      // Error message during compilation

      Note: Driver.java uses unchecked or unsafe operations.

      Note: Recompile with -Xlint:unchecked for details.

       

      // My code

       

      // Problem here: I try to add new elements to the list. It's the very last line of the method that results in the error. I tried various things such as:

      // model.addElement(<String> s);

      // but   so far nothing has worked. How do I add new elements to the list (model)? 

       

      public static void m2(JList <String> list)

          {

          String s;

          int i;

          String [] array = new String[10];

          DefaultListModel model = (DefaultListModel) list.getModel();

          if (model.elementAt(0) instanceof String)

              System.out.println("String");

          for (i = 5; i < 10; i++) {

              array[i] = i + "*";

              s = (String) model.getElementAt(i);

              model.addElement(s);  // problem is here

          }

       

          }

       

      // Code is OK: Create the array of strings to add to constructor of the JList

      public static String[]  m1()
      {

          String s;

          int i;

          String [] array = new String[10];

          for (i = 0; i < 5; i++)

          array[i] = i + "*";

          return(array);

      }

       

          public static void main(String [] args)

          {

          JFrame jf = null;

          GridBagLayout layout = new GridBagLayout();

          GridBagConstraints constraint = new GridBagConstraints();

          String [] data = m1();  // Creates and returns an array of Strings that is used as the list data

          JList<String>aList = new JList<String>(data);  // The change I had to make when compiling under the newer version of Java to indicate that the list would store strings

       

         // Things are okay here now but then when I try to add new elements to the model via method 'm2' that's where I get the compiler error

        • 1. Re: Adding elements to an existing JList under Java7
          rp0428
          I know there was a change in the later versions of Java where the C++ equivalent of a <template type> was added.

          Yes - that is called 'generics'. See The Java Tutorials

          http://docs.oracle.com/javase/tutorial/java/generics/

          Unfortunately the change has 'broken' my older code.

          No - it hasn't done any such thing. Older code doesn't have generics so generics can't break it.

          If I have a JList and I want to add elements to it then now I should specify the type e.g., the list will store Strings.

          Yes - IF you want to use generics. If you don't want to use generics then DON'T DO THAT!

          When I do this and then add data to the list (or actually the list model) the code is ''fixed". However if after adding those new elements to the list I later need to add more elements, which isn't unreasonable for a list...to have elements added dynamically at run time then I again get the same compiler error message that I haven't correctly specified the type:

          CORRECT! If you use generics and specify the type then Java will check that you have 'correctly specified the type'. What would you expect it to do?

           

          1. Create a box that can hold ANYTHING

          2. You can put Apples, Oranges, Baseballs or anything else in the box

           

          1. Create a box that can only hold APPLES

          2. You can put Apples in the box

          3. You can NOT put Oranges, Baseballs or anything else in the box

           

          Doesn't that make sense to you?

          public static void m2(JList <String> list)

          Ok - that 'list' can ONLY hold STRINGS - you are using generics. That is NOT old code; old code didn't have generics.

          DefaultListModel model = (DefaultListModel) list.getModel();

          Now you are using a 'model' that is NOT generic. You are using 'DefaultListModel' instead of 'DefaultListModel<E>'.

           

          But if you look at the API for the 'getModel' method you will see that it returns a 'generic' type.

          http://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html#getModel()

          public ListModel<E> getModel()

          Returns the data model that holds the list of items displayed by the JList component.

          Returns:
          the ListModel that provides the displayed list of items

          In your case the method is returning 'ListModel<String>' but you are throwing that away by casting it to a non-generic 'DefaultListModel'.

          model.addElement(s);  // problem is here

          Yes - problem is 'there'. You are mixing generic and non-generic uses so you get that error message telling you that.

           

          If you don't want to use generics then don't use them. If you want to use them then use them.

           

          But don't mix the two together.

           

          See The Java Tutorials trail for how to use lists

          http://docs.oracle.com/javase/tutorial/uiswing/components/list.html

          Unfortunately the change has 'broken' my older code

          No - nothing has broken your old code.

           

          But YOU have broken your new code.