NetBeans jList Add/Clear Items

J

jeff

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)
 
G

garethconner

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
 
A

Andrew Thompson

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.
 
R

rex64

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)
 
R

rex64

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)
 
A

Andrew Thompson

rex64 said:
It is still broken.

What? Your basic debugging skills?
... When I call the function I get a runtime error.

public void RefreshQuestionList()
{

Object o = jListQuestions.getModel();
System.out.println("It is " + o);
....

Andrew T.
 
R

rex64

When I run that debug code I get this as results. I am not sure what it
means and how to fix the problem:
It is studyx.AddQuestions$2@8ed465
 
R

rex64

Hey. I ran this and got the results below. I do not know what they mean
or how to fix it:
It is studyx.AddQuestions$2@8ed465
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top