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 / September 2003

Tip: Looking for answers? Try searching our database.

Cleared JList remembers last selection - ?

Thread view: 
Rick - 28 Sep 2003 21:06 GMT
I have a 'clear' JButton that when pressed clears a JList selection by
calling aListSelectionModel.clearSelection(). This is Fine.

However, it would seem that clearing the selection changes the list
visually but the model does not 'forget' the selection. Basically,
using the down arrow on a cleared JList seems to set the selected
index to the last selected index + 1. (Using the down arrow a list for
the first time - when nothing has been selected yet - selects item at
index 0!)

The (rough) code below illustrates this odd behavior. Select an item
from the list; press the clear button; tab back to the list. Am I not
clearing the selection the correct way? Comments?

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

public class ListTest extends JFrame {

    public ListTest() {
        super();
       
        JButton clearButton = new JButton("Clear Selection from List");
        String[] data = {"one", "two", "three", "four", "five",
"six","seven","eight","nine","ten"};
       
        final JList aList = new JList(data);
        JScrollPane sp = new JScrollPane(aList);
               
        getContentPane().add(clearButton, BorderLayout.CENTER);
        getContentPane().add(sp,BorderLayout.EAST);
       
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);}}
        );
        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                aList.getSelectionModel().clearSelection();
                }
            }
        );

        setSize(400,200);
    }
   
    public static void main(String[] args) {
        ListTest lt = new ListTest();
        lt.setVisible(true);
    }
}
Kleopatra - 29 Sep 2003 08:41 GMT
> However, it would seem that clearing the selection changes the list
> visually but the model does not 'forget' the selection. Basically,
> using the down arrow on a cleared JList seems to set the selected
> index to the last selected index + 1. (Using the down arrow a list for
> the first time - when nothing has been selected yet - selects item at
> index 0!)

navigation in the JList is based on the leadIndex of the selectionModel.
DefaultListSelectionModel does not update the lead properly on clearing
the
selection. It's an old issue showing in several bug reports (see f.i.
#4334792, #4338140, #4303294) - and marked as fixed for Tiger in some of
them.

Greetings
Jeanette
Rick - 29 Sep 2003 20:41 GMT
I was not aware that this was an existing problem.
Thanks for referring me to the bug report. I will look more carefully
there in the future!

Rick


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.