• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 821
  • Last Modified:

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?
0
Sapphireblue
Asked:
Sapphireblue
  • 5
  • 2
1 Solution
 
TimYatesCommented:
Don't use Vector

use a DefaultListModel:


DefaultListModel model ;

...

list = new JList() ;
model = new DefaultListModel( vector ) ;
list.setModel( model ) ;

...

then add, and remove from the model..
0
 
TimYatesCommented:
Hope that link helps explain what I mean :-)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
TimYatesCommented:
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...

:-)
0
 
SapphireblueAuthor Commented:
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]
0
 
SapphireblueAuthor Commented:
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.
0
 
TimYatesCommented:
Yay!

Glad you got it sorted!!!

Good luck!

Tim
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now