Friday, February 24, 2017

[Solved] java.lang.RuntimeException: Cannot use this method with a non-Mutable data model

The code below throws the java.lang.RuntimeException: Cannot use this method with a non-Mutable data model exception.

public class MyComboBoxModel<Student> implements ComboBoxModel<Student> {
      private ArrayList<Parent> parents;
      private DefaultComboBoxModel<Student> students;

      public MyComboBoxModel() {
            parents = new ArrayList<>();
            students = new DefaultComboBoxModel<>();
      }

      public MyComboBoxModel(Object[][] spPares) {
            parents = new ArrayList<>();
            students = new DefaultComboBoxModel<>();
            for (Object[] sp : spPares) {
                  students.addElement((Student)sp[0]);
                  parents.add((Parent)sp[1]);
           }
      }

      public Student getSelectedItem() {
            return (Student) students.getSelectedItem();
      }

      public Parent getSelectedParent() {
            int idx = students.getIndexOf(students.getSelectedItem());
            return parents.get(idx);
      }

      public void addElement(Object[] theV) {
            students.addElement((Student)theV[0]);
            parents.add((Parent)theV[1]);
      }

      public void insertElementAt(Object[] theV, int idx) {
            students.insertelementAt((Student) theV[0], idx);
            parents.add(idx, (Parent) theV[1]);
      }

      public void removeElement(Student s) {
            int idx = students.getIndexOf(s);
            parents.remove(idx);
            students.removeElement(s);
     }

     public void removeElementAt(int idx) {
           parent.remove(idx);
           students.removeElementAt(idx);
     }
}

public class Test extends JFrame{
     public Test() {
            JComboBox testBox = new JComboBox();
            testBox.setModel(new MyComboBoxModel());

            // this line of code throws the exception
            testBox.addElement(new Object[]{new Student("Tom"), new Parent("John")});

            getContentPane().add(testBox);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           setVisible(true);
     }

     public static void main (String[] args) {
           new Test();
     }
}

To fix this problem, change the first line of the MyComboBoxModel
from
       public class MyComboBoxModel<Student> implements ComboBoxModel<Student>
to
       public class MyComboBoxModel<Student> implements MutableComboBoxModel<Student>

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live




If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.

No comments:

Post a Comment