Sapphireblue
asked on
Repaint JList(Vector) when Vector contents are changed?
I have a JList(Vector) in a Swing app. The Vector (of objects) being used to populate the JList can be added onto via a modal dialog. When the dialog is used to add an object to the Vector, the Vector is correctly updated, but the JList that had been populated by the now-changed vector "disappears." Is there a way to re-paint or re-set the JList with the updated Vector contents?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://javaalmanac.com/egs/javax.swing/list_ListAddRem.html
Hope that link helps explain what I mean :-)
When you create a JList with a vector in the constructor, it creates an internal model, and copies the vector into it...so changing the vector has no influence on the list...
Just so you know why it isn't working as you have it...
:-)
Just so you know why it isn't working as you have it...
:-)
ASKER
yikes. trying it that way, I now get NullPointerExceptions all over the place. The JList sits next to a bunch of buttons that can do different things that relate to the contents of the JList; I can't even get a "show details of list item" type showMessageDialog to come up.
The following code was modified according to the info in the Java Almanac page; it now adds an item to the Vector AND tries to add an item to the list model I created. The former succeeds; the latter throws a big ugly NullPointerException.
In my class constructor:
studentListModel = new DefaultListModel(); // previously declared as instance variable
JList studentList = new JList(studentListModel);
for (int i=0; i<studentsVector.size(); i++)
{
studentListModel.add(i, studentsVector.get(i));
}
----------
In my actionPerformed method:
else if (source == newStudentButton)
{
NewStudentDialog newStudentDialog = new NewStudentDialog((JFrame)S wingUtilit ies.getRoo t(newStude ntButton)) ;
newStudentDialog.setVisibl e(true);
if(newStudentDialog.change Submitted == true)
{
Student tempStudent = new Student(newStudentDialog.g etIdentifi cationNumb er(),newSt udentDialo g.getFirst Name(),new StudentDia log.getLas tName());
studentsVector.add(tempStu dent); // this works
studentListModel.add(stude ntList.get Model().ge tSize(), tempStudent); // this bombs
}
}
----------
The stack trace helps me not a bit, I'm afraid:
java.lang.NullPointerExcep tion
at EnrollPanel.actionPerforme d(Enroll.j ava:217) // this refers to the line that bombs
at javax.swing.AbstractButton .fireActio nPerformed (AbstractB utton.java :17
67)
at javax.swing.AbstractButton $ForwardAc tionEvents .actionPer formed(Abs tra
ctButton.java:1820)
at javax.swing.DefaultButtonM odel.fireA ctionPerfo rmed(Defau ltButtonMo del
.java:419)
[etc etc etc]
The following code was modified according to the info in the Java Almanac page; it now adds an item to the Vector AND tries to add an item to the list model I created. The former succeeds; the latter throws a big ugly NullPointerException.
In my class constructor:
studentListModel = new DefaultListModel(); // previously declared as instance variable
JList studentList = new JList(studentListModel);
for (int i=0; i<studentsVector.size(); i++)
{
studentListModel.add(i, studentsVector.get(i));
}
----------
In my actionPerformed method:
else if (source == newStudentButton)
{
NewStudentDialog newStudentDialog = new NewStudentDialog((JFrame)S
newStudentDialog.setVisibl
if(newStudentDialog.change
{
Student tempStudent = new Student(newStudentDialog.g
studentsVector.add(tempStu
studentListModel.add(stude
}
}
----------
The stack trace helps me not a bit, I'm afraid:
java.lang.NullPointerExcep
at EnrollPanel.actionPerforme
at javax.swing.AbstractButton
67)
at javax.swing.AbstractButton
ctButton.java:1820)
at javax.swing.DefaultButtonM
.java:419)
[etc etc etc]
ASKER
Oh, I got it. Stupid mistake--in my constructor I was doing JList studentList = new JList(studentListModel); when I'd already declared studentList as an instance variable. My bad.
Thanks, Tim, for all your help.
Thanks, Tim, for all your help.
Yay!
Glad you got it sorted!!!
Good luck!
Tim
Glad you got it sorted!!!
Good luck!
Tim