Prev: Arkanoid / Breakout
Next: How to prevent sun.reflect.GeneratedSerializationConstructorAccessor from being unloaded at full gc
From: jeff on 12 Oct 2006 12:42 I am attempting to clear a jList in NetBeans and add new items. However, I get compile errors on all this. Let me know what a good way to do this would be. How can I add items if no add function is provided? I am confused. I was attempting to add using the examples I have seen in my research.I thought it would be easy, but I still do not know how to add things to a list box. Is this the correct documentation? http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/ListModel.html Or what is this: http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/DefaultListModel.html And what is this: http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/AbstractListModel.html public void RefreshQuestionList() { ListModel model = jListQuestions.getModel(); model.clear(); model.addElement("Alison Huml"); //listModel = new DefaultListModel(); //listModel.addElement("Alison Huml"); //jListQuestions.clear(); //jListQuestions.addElement("Alison Huml"); //jListQuestions.addElement(StudyX.questionList[0].answer); } init: deps-jar: Compiling 1 source file to C:\Reports\Jeff\java\StudyX\build\classes C:\Reports\Jeff\java\StudyX\src\studyx\AddQuestions.java:27: cannot find symbol symbol : method clear() location: interface javax.swing.ListModel model.clear(); C:\Reports\Jeff\java\StudyX\src\studyx\AddQuestions.java:28: cannot find symbol symbol : method addElement(java.lang.String) location: interface javax.swing.ListModel model.addElement("Alison Huml"); 2 errors BUILD FAILED (total time: 1 second)
From: garethconner on 12 Oct 2006 17:00 > How can I add items if no add function is provided? I am confused. I > was attempting to add using the > examples I have seen in my research.I thought it would be easy, but I > still do not know how to add things to a list box. The basic problem is that the ListModel interface does not contain a ..clear() method (which is what is mentioned in the compiler output. The DefaultListModel implementation does, in fact, have a clear() method. So you could set your JLists ListModel to an instance of DefaultListModel to take advantage of the clear() method. In Netbeans, from the GUI editor, select your JList and then edit the 'model' property from the property inspector. Click the 'advanced' tab and check "Generate Post Initialisation Code" then write: jListQuestions.setModel(new DefaultListModel()); You also need to change the first line in your RefreshQuestionsList method. To work it should read: DefaultListModel model = (DefaultListModel) jListQuestions.getModel(); Making those changes I was able to compile and execute your code in Netbeans. Hope that helps. -Gareth
From: Andrew Thompson on 12 Oct 2006 23:09 jeff(a)jeffcomputers.com wrote: > I am attempting to clear a jList in NetBeans .. As an aside, where did you get 'jList' from? There is a javax.swing.JList, but no jList (note capitalisation) in the J2SE. This is at least the second time I have seen 'jList' in association with that IDE, and am wondering whether - the IDE is referring to a JList as a jList, - it is referring to something entirely different, - it's users simply cannot spell, or - something else entirely.. Could you clarify? Andrew T.
From: rex64 on 13 Oct 2006 14:19 It is still broken. When I call the function I get a runtime error. public void RefreshQuestionList() { DefaultListModel model = (DefaultListModel) jListQuestions.getModel(); model.clear(); model.add(0,"Alison Huml"); //ListModel model = jListQuestions.getModel(); //model.clear(); // model.addElement("Alison Huml"); //listModel = new DefaultListModel(); //listModel.addElement("Alison Huml"); //jListQuestions.clear(); // jListQuestions.add("Alison Huml"); //jListQuestions.addElement(StudyX.questionList[0].answer); } init: deps-jar: Compiling 1 source file to C:\Reports\Jeff\java\StudyX\build\classes compile: run: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: studyx.AddQuestions$2 at studyx.AddQuestions.RefreshQuestionList(AddQuestions.java:27) at studyx.AddQuestions.jBtnAddActionPerformed(AddQuestions.java:143) at studyx.AddQuestions.access$000(AddQuestions.java:17) at studyx.AddQuestions$1.actionPerformed(AddQuestions.java:62) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:5488) at javax.swing.JComponent.processMouseEvent(JComponent.java:3126) at java.awt.Component.processEvent(Component.java:5253) at java.awt.Container.processEvent(Container.java:1966) at java.awt.Component.dispatchEventImpl(Component.java:3955) at java.awt.Container.dispatchEventImpl(Container.java:2024) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822) at java.awt.Container.dispatchEventImpl(Container.java:2010) at java.awt.Window.dispatchEventImpl(Window.java:1778) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
From: rex64 on 13 Oct 2006 14:19
It is still broken. When I call the function I get a runtime error. public void RefreshQuestionList() { DefaultListModel model = (DefaultListModel) jListQuestions.getModel(); model.clear(); model.add(0,"Alison Huml"); //ListModel model = jListQuestions.getModel(); //model.clear(); // model.addElement("Alison Huml"); //listModel = new DefaultListModel(); //listModel.addElement("Alison Huml"); //jListQuestions.clear(); // jListQuestions.add("Alison Huml"); //jListQuestions.addElement(StudyX.questionList[0].answer); } init: deps-jar: Compiling 1 source file to C:\Reports\Jeff\java\StudyX\build\classes compile: run: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: studyx.AddQuestions$2 at studyx.AddQuestions.RefreshQuestionList(AddQuestions.java:27) at studyx.AddQuestions.jBtnAddActionPerformed(AddQuestions.java:143) at studyx.AddQuestions.access$000(AddQuestions.java:17) at studyx.AddQuestions$1.actionPerformed(AddQuestions.java:62) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:5488) at javax.swing.JComponent.processMouseEvent(JComponent.java:3126) at java.awt.Component.processEvent(Component.java:5253) at java.awt.Container.processEvent(Container.java:1966) at java.awt.Component.dispatchEventImpl(Component.java:3955) at java.awt.Container.dispatchEventImpl(Container.java:2024) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822) at java.awt.Container.dispatchEventImpl(Container.java:2010) at java.awt.Window.dispatchEventImpl(Window.java:1778) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110) |