Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2005

Tip: Looking for answers? Try searching our database.

ListSelectionListener not showing state

Thread view: 
Ike - 31 Aug 2005 19:19 GMT
I have a JList with a DefaultListModel, and I add a ListSelectionListener to
my JList (code below). However, when I click on an item in the Jlist, it
doesn't alter the selected state of an item. Since valueChanged() is from an
interface, I cannot call super on it. Can someone please point out how I can
make my ListSelectionListener reflect the selected/deselected state of items
in it when I click upon them? Thanks, Ike

DefaultListModel listmodel_1=new DefaultListModel();
JList  nameDisplay=new JList(listmodel_1);

nameDisplay.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
;
nameDisplay.addListSelectionListener(new
nameDisplayListSelectionListener());

private class nameDisplayListSelectionListener implements
ListSelectionListener{
       public void valueChanged(ListSelectionEvent e){
           if(ignoreListListener){
                              return;
           }
           ignoreListListener=true;
           setCursor(new Cursor(Cursor.WAIT_CURSOR));
           JList j = (JList)(e.getSource());
           ListSelectionModel lsm = j.getSelectionModel();
           j.repaint();
           for(int i=0;i<qplayers.size();i++){
               Qplayer qp = (Qplayer)qplayers.get(i);
               if(qp.inlist){
                   if(!qp.available  && lsm.isSelectedIndex(i)){
                       qp.available=true;
                       lastauditstring=auditstring;
                       auditstring=qp.nick+" 1";
                       if(!lastauditstring.equals(auditstring))
                           say(auditstring);
                       break;
                   }else if(qp.available && !lsm.isSelectedIndex(i)){
                       qp.available=false;
                       lastauditstring=auditstring;
                       auditstring=qp.nick+" 0";
                       if(!lastauditstring.equals(auditstring))
                          say(auditstring);
                       break;
                   }
               }else{
                   say(qp.nick+" -1");
                   break;
               }
           }

           setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
           if(whoIsLosingIt!=null)
               whoIsLosingIt.grabFocus();
           ignoreListListener=false;

       }
   }
Thomas Hawtin - 31 Aug 2005 20:15 GMT
> I have a JList with a DefaultListModel, and I add a ListSelectionListener to
> my JList (code below). However, when I click on an item in the Jlist, it
> doesn't alter the selected state of an item. Since valueChanged() is from an
> interface, I cannot call super on it. Can someone please point out how I can
> make my ListSelectionListener reflect the selected/deselected state of items
> in it when I click upon them? Thanks, Ike

Are you suggesting that adding any selection listener to a JList stops
selection changing? Why not try a little test.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

class SelectionTest {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() { public void run() {
                      swing();
        }});
    }
    private static void swing() {
        JFrame frame = new JFrame("SelectionTest");
        frame.setDefaultCloseOperation(
            WindowConstants.DISPOSE_ON_CLOSE
        );
        JList list = new JList(new Object[] {
                "Fred", "Jim", "Sheila"
        });
        list.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent event) {
                    //throw new Error();
                }
            }
        );
        frame.add(list);
        frame.pack();
        frame.setVisible(true);
    }
}

Works for me. Although things go a little astray if it throws an error.
I do get copious notification in the console, however. And it repaints
differently if I move a window over it.

> DefaultListModel listmodel_1=new DefaultListModel();

Not a great name.

> JList  nameDisplay=new JList(listmodel_1);
> nameDisplay.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
[quoted text clipped - 4 lines]
>  private class nameDisplayListSelectionListener implements
> ListSelectionListener{

Another less than inspiring name.

>         public void valueChanged(ListSelectionEvent e){
>             if(ignoreListListener){
>                                return;
>             }
>             ignoreListListener=true;

If you're going to do this, do make sure you reset the flag within a
finally block. If your listener throws an exception you are going to be
in a state.

>             setCursor(new Cursor(Cursor.WAIT_CURSOR));

You shouldn't be blocking the Event Dispatch Thread (EDT), so this
shouldn't be necessary.

>             JList j = (JList)(e.getSource());

Nice name.

>             ListSelectionModel lsm = j.getSelectionModel();

It's perhaps not great to have to decode acronyms. It's the selectionModel.

>             j.repaint();

Utterly pointless, unless you are planning on throwing an exception.

>             for(int i=0;i<qplayers.size();i++){
>                 Qplayer qp = (Qplayer)qplayers.get(i);
>                 if(qp.inlist){

Public variables?

>                     if(!qp.available  && lsm.isSelectedIndex(i)){
>                         qp.available=true;
>                         lastauditstring=auditstring;

Have we set auditstring to anything nice?

>                         auditstring=qp.nick+" 1";
>                         if(!lastauditstring.equals(auditstring))
[quoted text clipped - 17 lines]
>             if(whoIsLosingIt!=null)
>                 whoIsLosingIt.grabFocus();

Huh?

>             ignoreListListener=false;
>
>         }
>     }

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.