🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
2 messages
2 total messages Started by Alan Shiers Sat, 26 Mar 2005 01:53
JList problems
#622
Author: Alan Shiers
Date: Sat, 26 Mar 2005 01:53
66 lines
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
Re: JList problems
#625
Author: John McGrath [TeamB]
Date: Sat, 26 Mar 2005 22:47
31 lines
1849 bytes
On 3/25/2005 at 8:53:30 PM, Alan Shiers wrote:

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

   public void clear() {
      int index1 = delegate.size()-1;
      delegate.removeAllElements();
      if (index1 >= 0) {
         fireIntervalRemoved(this, 0, index1);
      }
   }

As you can see, it does fire the appropriate event.

> If anyone knows of anything I've overlooked in so far as my first
> approach, I'd like to hear from you.

It is hard to tell what is going wrong from just a description of what you
think the code is doing.  If you post a runnable example of the problem, I
will be glad to take a look at it.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads