Link to home
Start Free TrialLog in
Avatar of Sapphireblue
SapphireblueFlag for United States of America

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
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...

:-)
Avatar of Sapphireblue

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)SwingUtilities.getRoot(newStudentButton));
      newStudentDialog.setVisible(true);
      if(newStudentDialog.changeSubmitted == true)
      {
            Student tempStudent = new Student(newStudentDialog.getIdentificationNumber(),newStudentDialog.getFirstName(),newStudentDialog.getLastName());
            studentsVector.add(tempStudent);    // this works
            studentListModel.add(studentList.getModel().getSize(), tempStudent);   // this bombs
      }
}

----------

The stack trace helps me not a bit, I'm afraid:

java.lang.NullPointerException
        at EnrollPanel.actionPerformed(Enroll.java:217)  // this refers to the line that bombs
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:17
67)
        at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
ctButton.java:1820)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:419)
        [etc etc etc]
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.
Yay!

Glad you got it sorted!!!

Good luck!

Tim