?
Solved

Repaint JList(Vector) when Vector contents are changed?

Posted on 2005-03-17
7
Medium Priority
?
819 Views
Last Modified: 2009-12-16
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
Comment
Question by:Sapphireblue
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 2000 total points
ID: 13566815
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13566830
Hope that link helps explain what I mean :-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Expert Comment

by:TimYates
ID: 13566919
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
 
LVL 1

Author Comment

by:Sapphireblue
ID: 13567671
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
 
LVL 1

Author Comment

by:Sapphireblue
ID: 13567740
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13567958
Yay!

Glad you got it sorted!!!

Good luck!

Tim
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question