🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #9558

Re: Vectors

#9558
From: James Cook
Date: Mon, 28 Feb 2000 00:00
65 lines
2777 bytes
Your example code is a little odd. You use:

>     list = new JList(vTest);

when the vector's name is empArray.

Anyway, the problem you are seeing is related to the fact that the Vector
that you pass the JList is only used in the JList's initialization. Any
changes to the Vector after that point do not affect the JList, as you've
found out.

A JList is driven by a javax.swing.ListModel, or in your specific case, a
javax.swing.AbstractListModel. This class stores a reference to your vector
(marked as final, so the reference cannot change.), and feeds the JList
information about the vector. If you make changes to your Vector, however,
the AbstractListModel cannot detect them. Therefore, it cannot tell the
JList that something has changed.

If you are using the Vector only as a storage device, I might suggest that
you use a javax.swing.DefaultListModel instead. In fact, none of your source
should change. It wrappers a Vector and fires the apporpriate events so the
JList stays in sync with the datastructure.

jim


Keith Underwood <keithu@gte.net> wrote in message
news:38B92F4B.D0D0FD06@gte.net...
> I have created a a Vector to pass into a JList which will be displayed
> in a JScrollPane
>
> I declare it in my main frame
>   static JList list = null;
>   static JScrollPane scrollPane = null;
>   static Vector empArray;
>
> I initialize them in jbInit()
>     Vector empArray = new Vector();
>     list = new JList(vTest);
>     scrollPane = new JScrollPane(list);
>
> I add some elements in jbInit()
>     empArray.addElement("Billy");
>     empArray.addElement("George");
>
> This works fine and the main frame shows a JScrollPane with Billy and
> George in it.
>
> I have to add more elements after the program starts and I keep getting
> a NullPointer  Exception.
> I can find no way to add more elements to the Vector and have them
> display in the JList, unless I do
> it in the jbInit().
>
> I have even created a fake button that has.
>
> empArray.addElement("Homer");
> When the button is pressed I get the NP Exception
>
> Any clues as to how to add elements to my vector to be displayed in th
> JScrollPane
>
> Keith
>
>
Technical Details
Message-ID: <38ba80b4.0@news.dot.state.oh.us>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <38B92F4B.D0D0FD06@gte.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,d0b66bd46418f857,start
X-Google-Attributes: gid111d45,public
From: "James Cook" <jimcook.spaminator@remove.for.spam.iname.com>
Subject: Re: Vectors
Date: 2000/02/28
Message-ID: <38ba80b4.0@news.dot.state.oh.us>
X-Deja-AN: 590929217
References: <38B92F4B.D0D0FD06@gte.net>
X-Trace: 28 Feb 2000 09:05:40 -0500, dotfw001-dmz.dot.state.oh.us
X-MSMail-Priority: Normal
X-Priority: 3
Newsgroups: borland.public.jbuilder.java.swing
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300