Article View: borland.public.jbuilder.java.swing
Article #622JList problems
From: Alan Shiers
Date: Sat, 26 Mar 2005 01:53
Date: Sat, 26 Mar 2005 01:53
66 lines
3229 bytes
3229 bytes
Hi there,
I think someone there at SUN needs to fix the DefaultListModel or JList
itself because I was having a heck of a time trying to make JList display
changes. revalidate() and repaint() methods were useless. Allow me to
explain...
I created an applet that contained two JScrollPanes each containing a JList.
Between these two JScrollPanes I have four buttons labelled: <, >, << and
>>
For the buttons labelled << and >> the actionPerformed method was to simply
move all the contents from one JList over to the other JList. These were
the buttons I concentrated on first.
I started by declaring global variables named:
JList jList1 = new JList();
JList jList2 = new JList();
DefaultListModel listModel1 = new DefaultListModel();
DefaultListModel listModel2 = new DefaultListModel();
String[] list = null;
In the init() method of my applet I set the models for each JList:
jList1.setModel(listModel1);
jList2.setModel(listModel2);
I populate the first JList jList1 with an array of Strings using:
list = new String[20];
list[0] = "1234 John Lennon";
list[1] = "2345 Paul McCartney";
list[2] = "3456 Ringo Starr";
list[3] = "4567 George Harrison";
...
jList1.setListData(list);
Then for the button labelled ">>" I wanted to move all the data from jList1
to jList2 while removing all elements from jList1.
The actionPerformed(ActionEvent e) method reads:
else if(e.getSource() == jButton5) //Move all left to right
{
jList2.setListData(list); //<< this works fine, jList2 gets its data
listModel1.clear(); //<< this works but doesn't refresh the display by
itself !
System.out.println("jList1 is empty = " + listModel1.isEmpty());
//isEmpty() returns true...that's good!
jList1.revalidate(); //<< doesn't work!!! jList1 still is
displaying the data in String array list
}
I finally got it to work but I think it's a poor way of doing things. This
is how I had to rewrite it:
else if(e.getSource() == jButton5) //Move all left to right
{
jList2.setListData(list);
jList1.setListData(new String[0]);
}
In my opinion, the clear() method of the DefaultListModel should fire
whatever methods it takes to refresh the JList just as the setListData(...)
method of the JList does.
If anyone knows of anything I've overlooked in so far as my first approach,
I'd like to hear from you.
Alan Shiers
Technical Details
Message-ID:
<4244c27d$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!newshub.sdsu.edu!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 111d45,db236ef79ca42222,start X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Alan Shiers" <ashiers@hfx.eastlink.ca> Newsgroups: borland.public.jbuilder.java.swing Subject: JList problems Date: Fri, 25 Mar 2005 21:53:30 -0400 Lines: 68 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 NNTP-Posting-Host: 24.222.101.2 Message-ID: <4244c27d$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1111802493 24.222.101.2 (25 Mar 2005 18:01:33 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!newshub.sdsu.edu!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail