Article View: borland.public.jbuilder.java.swing
Article #8019Re: JList only showing one selection
From: Kevin Dwan
Date: Tue, 07 Nov 2000 00:00
Date: Tue, 07 Nov 2000 00:00
61 lines
3226 bytes
3226 bytes
"Joi Ellis [TeamB]" wrote:
> Kevin Dwan <kevin@blueberry.com> wrote:
>
> > The problem may be that the default list model of a JList is immutable,
> > and I have to do something like set the JList to DefaultListModel with the
> > setModel method, and then zap the choices by operating on the instance
> > of the ListModel. I tried doing this using the ListModel's .clear()
> > and .removeAllElements() methods, but they don't seem to work as I
> > expected either.
>
> > I didn't think it would be this difficult to just erase the list.
>
> Ah, yes, you need to define your own AbstractDataModel and use one of the
> other JList constructors. It didn't occur to me to ask which constructor
> you were using. Have you checked Sun's tutorial pages for JList? It
> isn't really that difficult to create a model of your own. I've made
> several at work so far.
>
> --
> Joi Ellis (TeamB)
> No direct email, please.
> For best results, post on news server newsgroups.borland.com!
Yes, I've read the Tutorial. It says, "To create a list whose items can be
changed individually, set the list's model to an instance of a mutable list
model class, such as an insteance of DefaultListModel. You can set a list's
model when you create the list or by calling the setModel method."
This is what I tried:
JFileChooser sfc = new JFileChooser();
.
.
void approveButton_actionPerformed(ActionEvent e)
{
DefaultListModel hank = new DefaultListModel();
JList1.setModel(hank);
JList1.clearSelection(); //doesn�t clear JList1
hank.clear(); //doesn�t clear JList1
hank.removeAllElements();//doesn�t clear JList1
File [] sourceFiles = sfc.getSelectedFiles();
String[] namelist = new String [sourceFiles.length];
for (int i = 0; i < sourceFiles.length; i++){
namelist[i] = sourceFiles[i].getName();
for (int a = 0; a < namelist.length; a++){
hank.addElement(namelist[a]);
}
}
JList JList1 = new JList(hank);
//JList JList1 = new JList(namelist); //used this approach earlier
targetPanel.add(JList1, new XYConstraints(14, 17, 210, 279));
for (int a=0; a<JList1.getModel().getSize();a++){
System.out.println(JList1.getModel().getElementAt(a)); //diagnostic
}
}
What this does is update the JList, as I can see by the System.out.println
diagnostic, but JList1 in the GUI doesn't get updated at runtime.
Technical Details
Message-ID:
<3A089F21.40D91144@blueberry.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3A021239.92AF1FD4@blueberry.com> <8tt9hf$kml3@bornews.borland.com> <3A0363E0.2BF1797E@blueberry.com> <8tvor2$dfj4@bornews.borland.com> <3A075600.642F986E@blueberry.com> <8u88oh$2q10@bornews.inprise.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 111d45,34638f00403429f8 X-Google-Attributes: gid111d45,public From: Kevin Dwan <kevin@blueberry.com> Subject: Re: JList only showing one selection Date: 2000/11/07 Message-ID: <3A089F21.40D91144@blueberry.com> X-Deja-AN: 691016044 Content-Transfer-Encoding: 8bit References: <3A021239.92AF1FD4@blueberry.com> <8tt9hf$kml3@bornews.borland.com> <3A0363E0.2BF1797E@blueberry.com> <8tvor2$dfj4@bornews.borland.com> <3A075600.642F986E@blueberry.com> <8u88oh$2q10@bornews.inprise.com> X-Accept-Language: en Content-Type: text/plain; charset=iso-8859-1 Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing